python-dependency-injector/examples/miniapps/fastapi-simple/tests.py

25 lines
595 B
Python
Raw Normal View History

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
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"}