How do I override the FastAPI dependencies in a test? #208
|
I understand how to manually override and inject parameters into a method, but how do I override them for a test run? When testing FastAPI endpoints, I can override the dependencies with dependency_overrides. Right now, I have this: In the above, in a test, I can call like this: But what I want is for this to work: Is there some kind of override context that I can hook into when using @Injectable ? |
Replies: 1 comment 2 replies
|
Hi, You can refer to this example. So you will still need to prepare a FastAPI app object even if it's not testing a FastAPI endpoint, then call |
Hi,
You can refer to this example.
So you will still need to prepare a FastAPI app object even if it's not testing a FastAPI endpoint, then call
register_app(app), finally useapp.dependency_overrides[dependencies.PublishingService] = lambda: mock_publishing_serviceto override the dependency getter function.