Skip to content

dependency_overrides cannot be used in tests as expected, documentation lacking #19

@languitar

Description

@languitar

Describe the bug

Without fastapi-versioning, dependency_overrides can easily be specified on the app contained in the TestClient a la (pytest fixture like pseudo code):

before = client.app.dependency_overrides
client.app.dependency_overrides = dict(overrides)
try:
    yield
finally:
    client.app.dependency_overrides = before

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:

from fastapi import APIRouter, Depends, FastAPI
from fastapi.testclient import TestClient
from fastapi_versioning import versioned_api_route, VersionedFastAPI

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

app.include_router(router)
app = VersionedFastAPI(app)


def test_this() -> None:
    client = TestClient(app)
    client.app.dependency_overrides = {dependency: lambda: "patched"}
    assert client.get("/v1_0/test").json() == "patched"

This test will fail

Expected behavior

It should at least be documented how to access the correct sub application for providing the dependency_overrides.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions