-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dependency_overrides cannot be used in tests as expected, documentation lacking #19
Comments
Hello there... Having the same issue here. Spent literally two days of trying to make tests work, to realize it's because of VersionedAPI... :( Thanks! |
I was running into the same issue and realized it's caused by the order of operations:
The issue is when you create the routes in step 2, they are "compiled" and still reference the original FastAPI app from step 1 even when the dependencies are resolved when you make a request in step 6, so you have to add the dependency overrides to the original app: from fastapi import APIRouter, Depends, FastAPI
from fastapi.testclient import TestClient
from fastapi_versioning import versioned_api_route, VersionedFastAPI
original_app = FastAPI(title="test-app")
router = APIRouter(route_class=versioned_api_route(1, 0))
def dependency() -> str:
return "original"
@router.get("/test")
def get_test(dep: str = Depends(dependency)) -> str:
return dep
original_app.include_router(router)
app = VersionedFastAPI(original_app)
def test_this() -> None:
client = TestClient(app)
original_app.dependency_overrides = {dependency: lambda: "patched"}
assert client.get("/v1_0/test").json() == "patched" |
Describe the bug
Without fastapi-versioning,
dependency_overrides
can easily be specified on the app contained in theTestClient
a la (pytest fixture like pseudo code):With the internal use of sub application mounts, this doesn't work anymore and the override never reaches one of the versioned sub applications.
To Reproduce
Steps to reproduce the behavior:
This test will fail
Expected behavior
It should at least be documented how to access the correct sub application for providing the dependency_overrides.
The text was updated successfully, but these errors were encountered: