Skip to content

Commit

Permalink
Add testing to readme/getting started
Browse files Browse the repository at this point in the history
  • Loading branch information
maldoinc committed Oct 12, 2024
1 parent 10f0a3f commit 799020c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
16 changes: 15 additions & 1 deletion docs/pages/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ class WeatherService:
```



### 3. Use

Now you can use the container as a service locator or apply it as a decorator.
Expand All @@ -103,6 +102,21 @@ async def get_forecast_view(weather_service: WeatherService):
return await weather_service.get_forecast(...)
```

### 4. Test

Wireup does not patch your services which means they can be instantiated and tested independently of the container.

To substitute dependencies on autowired targets such as views in a web application you can override dependencies with new ones on the fly.


```python
with container.override.service(WeatherService, new=test_weather_service):
response = client.get("/weather/forecast")
```

Requests to inject `WeatherService` during the lifetime of the context manager
will result in `test_weather_service` being injected instead.

## Conclusion

This concludes the "Getting Started" walkthrough, covering the most common dependency injection use cases.
Expand Down
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ def get_weather_forecast_view(weather_service: WeatherService, request):
return weather_service.get_forecast(request.lat, request.lon)
```

**4. Test**

Wireup does not patch your services which means they can be instantiated and tested independently of the container.

To substitute dependencies on autowired targets such as views in a web application you can override dependencies with new ones on the fly.

```python
with container.override.service(WeatherService, new=test_weather_service):
response = client.get("/weather/forecast")
```

Requests to inject `WeatherService` during the lifetime of the context manager
will result in `test_weather_service` being injected instead.

## Share service layer betwen app/api and cli

Many projects have a web application as well as a cli in the same project which
Expand Down

0 comments on commit 799020c

Please sign in to comment.