Skip to content

Commit 5a72a9d

Browse files
committed
Simplify container override api
1 parent 7dc4310 commit 5a72a9d

20 files changed

Lines changed: 240 additions & 145 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ Wireup decorators only collect metadata. Injectables are plain classes and funct
432432
Swap dependencies during tests with `container.override`:
433433

434434
```python
435-
with container.override.injectable(target=Database, new=in_memory_database):
435+
with container.override({Database: in_memory_database}):
436436
# Injectables that depend on Database will receive in_memory_database
437437
# for the duration of this context manager
438438
response = client.get("/users")

docs/pages/container.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ db_url = container.config.get("database_url")
135135
Substitute dependencies for testing. Access via `container.override`.
136136

137137
```python
138-
with container.override.injectable(target=Database, new=mock_db):
138+
with container.override({Database: mock_db}):
139139
... # All injections of Database use mock_db
140140
```
141141

docs/pages/getting_started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ To substitute dependencies on targets such as views in a web application you can
254254
the fly.
255255

256256
```python
257-
with container.override.injectable(WeatherService, new=test_weather_service):
257+
with container.override({WeatherService: test_weather_service}):
258258
response = client.get("/weather/forecast")
259259
```
260260

docs/pages/integrations/aiohttp/index.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,9 @@ def test_override(aiohttp_client):
149149
def greet(self, name: str) -> str:
150150
return f"Hi, {name}"
151151

152-
with get_app_container(app).override.injectable(
153-
GreeterService,
154-
new=DummyGreeter(),
155-
):
152+
container = get_app_container(app)
153+
154+
with container.override({GreeterService: DummyGreeter()}):
156155
res = aiohttp_client.get("/greet?name=Test")
157156
```
158157

docs/pages/integrations/asgi/generic_asgi.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ def create_app():
8080

8181
def test_override():
8282
app = create_app()
83-
with app.state.container.override.injectable(
84-
MyService, new=MyFakeService()
85-
):
83+
with app.state.container.override({MyService: MyFakeService()}):
8684
...
8785
```
8886

docs/pages/integrations/click/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def test_random_number_command():
9292
return 4
9393

9494
# Create test container with mocked service
95-
with container.override.injectable(RandomService, new=MockRandomService()):
95+
with container.override({RandomService: MockRandomService()}):
9696
runner = CliRunner()
9797
result = runner.invoke(cli, ["random-number"])
9898

docs/pages/integrations/django/request_time_injection.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,4 @@ Prefer `@inject` for request-time functions and `@inject_app` for non-request en
101101
See [Django Testing](testing.md) for endpoint tests and dependency overrides. These patterns are tested the same way:
102102

103103
- call endpoints with `Client` or `AsyncClient`
104-
- use `get_app_container().override.injectable(...)` to inject fakes
104+
- use `get_app_container().override({...})` to inject fakes

docs/pages/integrations/django/testing.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ def test_override_http_endpoint():
6262
return f"Hi {name}"
6363

6464
client = Client()
65-
with get_app_container().override.injectable(
66-
GreeterService, new=FakeGreeter()
67-
):
65+
container = get_app_container()
66+
67+
with container.override({GreeterService: FakeGreeter()}):
6868
response = client.get("/greet/?name=World")
6969

7070
assert response.status_code == 200
@@ -87,9 +87,9 @@ def test_override_command():
8787
return f"Hi {name}"
8888

8989
out = StringIO()
90-
with get_app_container().override.injectable(
91-
GreeterService, new=FakeGreeter()
92-
):
90+
container = get_app_container()
91+
92+
with container.override({GreeterService: FakeGreeter()}):
9393
call_command("greet", "--name=World", stdout=out)
9494

9595
assert out.getvalue().strip() == "Hi World"
@@ -101,4 +101,4 @@ DRF and Ninja handlers should use `@inject` explicitly. Their tests follow the s
101101

102102
- call endpoint via test client
103103
- assert response
104-
- use `override.injectable(...)` when needed
104+
- use `container.override({...})` when needed

docs/pages/integrations/fastapi/class_based_handlers.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ from wireup.integration.fastapi import get_app_container
122122

123123

124124
def test_user_handler(app):
125-
with get_app_container(app).override.injectable(
126-
UserProfileService, new=MockUserService()
127-
):
125+
container = get_app_container(app)
126+
127+
with container.override({UserProfileService: MockUserService()}):
128128
# Start the client INSIDE the override block
129129
# The handler is initialized with the mock during startup
130130
with TestClient(app) as client:
@@ -133,11 +133,11 @@ def test_user_handler(app):
133133

134134
```python title="Don't"
135135
def test_user_handler_wrong(app):
136+
container = get_app_container(app)
137+
136138
# Handler has already been instantiated at startup.
137139
with TestClient(app) as client:
138-
with get_app_container(app).override.injectable(
139-
UserProfileService, new=MockUserService()
140-
):
140+
with container.override({UserProfileService: MockUserService()}):
141141
client.get("/users/")
142142
```
143143

docs/pages/integrations/fastapi/request_time_injection.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,11 @@ def test_require_auth_denied():
173173

174174

175175
def test_require_auth_allowed():
176+
container = get_app_container(app)
177+
176178
with TestClient(app) as client:
177179
# Override AuthService to return True
178-
with get_app_container(app).override.injectable(
179-
AuthService, new=MockAuthService(allow=True)
180-
):
180+
with container.override({AuthService: MockAuthService(allow=True)}):
181181
response = client.get("/users")
182182
assert response.status_code == 200
183183
```
@@ -193,8 +193,7 @@ def test_request_middleware_runs():
193193

194194
!!! tip
195195

196-
Use `get_app_container(app).override.injectable()` to inject mocks and fakes during tests. This works for both route
197-
decorators and middleware.
196+
Use `get_app_container(app).override()` to inject mocks and fakes during tests. This works for both route decorators and middleware.
198197

199198
## Direct Container Access
200199

0 commit comments

Comments
 (0)