2020-11-18 19:13:31 +03:00
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from httpx import AsyncClient
|
|
|
|
|
|
|
|
from fastapi_di_example import app, container, Service
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2021-11-01 03:31:39 +03:00
|
|
|
async def client(event_loop):
|
|
|
|
async with AsyncClient(app=app, base_url="http://test") as client:
|
|
|
|
yield client
|
2020-11-18 19:13:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_index(client):
|
|
|
|
service_mock = mock.AsyncMock(spec=Service)
|
2021-10-01 02:08:49 +03:00
|
|
|
service_mock.process.return_value = "Foo"
|
2020-11-18 19:13:31 +03:00
|
|
|
|
|
|
|
with container.service.override(service_mock):
|
2021-10-01 02:08:49 +03:00
|
|
|
response = await client.get("/")
|
2020-11-18 19:13:31 +03:00
|
|
|
|
|
|
|
assert response.status_code == 200
|
2021-10-01 02:08:49 +03:00
|
|
|
assert response.json() == {"result": "Foo"}
|