2020-11-18 19:13:31 +03:00
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
import pytest
|
2024-12-08 19:53:29 +03:00
|
|
|
import pytest_asyncio
|
2024-12-07 19:38:08 +03:00
|
|
|
from httpx import ASGITransport, AsyncClient
|
2020-11-18 19:13:31 +03:00
|
|
|
|
|
|
|
from fastapi_di_example import app, container, Service
|
|
|
|
|
|
|
|
|
2024-12-08 19:53:29 +03:00
|
|
|
@pytest_asyncio.fixture
|
|
|
|
async def client():
|
2024-12-07 19:38:08 +03:00
|
|
|
async with AsyncClient(
|
|
|
|
transport=ASGITransport(app=app),
|
|
|
|
base_url="http://test",
|
|
|
|
) as client:
|
2021-11-01 03:31:39 +03:00
|
|
|
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"}
|