# 断言错误：依赖覆盖未生效，测试使用了真实数据库连接。

- **ID:** `python/fastapi-dependency-overrides-not-applied`
- **领域:** python
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

app.dependency_overrides 使用的键与路由中实际使用的函数对象不同（例如模块被重新导入或依赖被包装）。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 0.100.x | active | — | — |
| 0.110.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   from app.deps import get_db
app.dependency_overrides[get_db] = lambda: test_session
client = TestClient(app)
   ```
2. **** (90% 成功率)
   ```
   Use `app.dependency_overrides.clear()` in teardown to avoid leakage between tests.
   ```

## 无效尝试

- **** — FastAPI requires the callable object, not its name. (95% 失败率)
- **** — TestClient uses the app passed to it; overrides elsewhere are ignored. (85% 失败率)
