question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support FastAPI dependency overrides

See original GitHub issue

Hi Steve,

Love the framework so far. One issue I’m having is in trying to use FastAPI dependency overrides in a similar manner to vanilla FastAPI, when making web-layer unit tests. I.e. mocking out dependencies.

In vanilla FastAPI you would do something like this (with pytest):

...
from fastapi.testclient import TestClient
from dependencies import get_foo_service  # <-- Dependency function
from foo import FooService
...

@pytest.fixture
def foo_service_mock():
    return Mock(FooService)

@pytest.fixture
def client(foo_service_mock) -> Generator:
    app.dependency_overrides[get_foo_service] = lambda: foo_service_mock

    with TestClient(app) as c:
        yield c

But I can’t see how I could replicate the override (app.dependency_overrides[get_foo_service]) functionality when using the Lagom FastAPI DI approach (i.e. when using deps.depends(FooService)).

I suppose I would somehow need to reference the inner _resolver function of depends…surely this isn’t possible?

Thus it would be nice if there were explicit support for FastAPI dependency overrides.

Thanks!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
meadstevecommented, Nov 25, 2021

The work in #177 is now released as version 1.6.0. Let me know how you get on @rowanbarrie . I’ll close this issue but we can re-open it if there’s some use-cases I’ve not covered.

1reaction
meadstevecommented, Nov 24, 2021

I went for a slightly different approach in #177 and returned a mutable container from the context manager rather than providing an update function/lambda. It felt easier to use and read.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing Dependencies with Overrides - FastAPI
For these cases, your FastAPI application has an attribute app.dependency_overrides , it is a simple dict . To override a dependency for testing,...
Read more >
FastAPI - Overriding dependencies while running tests
FastAPI - Overriding dependencies while running tests ... FastAPI tip: You can override a dependency of your app while running tests with dependency_overrides...
Read more >
Best way to override FastAPI dependencies for testing with a ...
If you want to override a dependency only during some tests, you can set the override at the beginning of the test (inside...
Read more >
[QUESTION] How to get dependency_overrides to work #641
I'm trying to use dependency_overrides to override a function for testing, but I can't get it to work. Here's a basic example.
Read more >
7. Dependency Injection in FastAPI - FastapiTutorial
FastAPI provides an elegant way to override the dependencies. Let's create a test_dependency_injection.py file and write the below code. Copy from fastapi.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found