python
config_error
ai_generated
true
AssertionError: 依赖覆盖未生效:响应仍使用生产数据库
AssertionError: Dependency overrides not applied: response still uses production DB
ID: python/fastapi-dependency-override-not-applied
80%修复率
86%置信度
0证据数
2024-10-02首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 0.100.x | active | — | — | — |
| 0.110.x | active | — | — | — |
根因分析
在测试中使用的app实例与dependency_overrides设置的实例不同,或者未在测试后清理覆盖。
English
Using `app.dependency_overrides` on a different app instance than the one under test, or not clearing overrides after.
解决方案
-
95% 成功率
client = TestClient(app)\napp.dependency_overrides[get_db] = fake_db
-
93% 成功率
@pytest.fixture\ndef client():\n yield TestClient(app)\n app.dependency_overrides.clear()
无效尝试
常见但无效的做法:
-
70% 失败
If the TestClient uses a different app, overrides never apply.
-
80% 失败
FastAPI resolves via dependency_overrides, not module lookup.