@@ -44,50 +44,27 @@ async def some_service_mock(
4444 settings.EXTERNAL_SERVICE_URL = old_url
4545```
4646
47- ### 2. mock specific api
47+ ### 2. mock and test it
4848
49- You don’t need to follow this pattern exactly —
50- this is just an example where the fixture is responsible for mocking
51- a specific route.
52-
53- ``` python
54- @pytest.fixture
55- def some_service_mock_api (
56- some_service_mock : AddMockDataFunc,
57- ) -> Callable[
58- [web.Response | ResponseHandler],
59- List[dict[str , Any]],
60- ]:
61- """ An example of a fixture where a specific API is mocked"""
62-
63- def _create_mock (
64- response : web.Response
65- | Callable[[web.Request], web.Response | Awaitable[web.Response]],
66- ) -> List[dict[str , Any]]:
67- return some_service_mock(MockData(" POST" , " /some_api" , response))
68-
69- return _create_mock
70- ```
71-
72- ### 3. test it
49+ #### static mock
7350
7451``` python
7552import pytest
7653from http import HTTPStatus
77-
54+ from async_pytest_httpserver import (
55+ MockData,
56+ )
7857from aiohttp.web import json_response, Request, Response
7958
80- # example of static mock
8159
8260@pytest.mark.asyncio
83- async def test_static_mock (client , some_service_mock_api ):
61+ async def test_static_mock (client , some_service_mock ):
8462 # Arrange
85- calls_info = some_service_mock_api(
86- json_response(
87- {" result" : " some_result" },
88- status = HTTPStatus.OK ,
89- )
63+ response = json_response(
64+ {" result" : " some_result" },
65+ status = HTTPStatus.OK ,
9066 )
67+ calls_info = some_service_mock(MockData(" POST" , " /some_api" , response))
9168
9269 # Act
9370 response = await client.post(
@@ -103,20 +80,32 @@ async def test_static_mock(client, some_service_mock_api):
10380 assert len (calls_info) == 1
10481 call_info = calls_info[0 ]
10582 assert call_info[" json" ] == {" text" : " text" }
83+ ```
84+
85+ #### dynamic async mock
10686
87+ ``` python
88+ import pytest
89+ from http import HTTPStatus
90+ from async_pytest_httpserver import (
91+ MockData,
92+ )
93+ from aiohttp.web import json_response, Request, Response
10794
108- # example of dynamic async mock
10995
11096async def async_mock_handler (request : Request) -> Response:
11197 return json_response(
112- {" result" : " some_result" },
113- status = HTTPStatus.OK ,
114- )
98+ {" result" : " some_result" },
99+ status = HTTPStatus.OK ,
100+ )
101+
115102
116103@pytest.mark.asyncio
117- async def test_async_handler (client , some_service_mock_api ):
104+ async def test_async_handler (client , some_service_mock ):
118105 # Arrange
119- calls_info = some_service_mock_api(async_mock_handler)
106+ calls_info = some_service_mock(
107+ MockData(" POST" , " /some_api" , async_mock_handler)
108+ )
120109
121110 # Act
122111 response = await client.post(
@@ -132,21 +121,31 @@ async def test_async_handler(client, some_service_mock_api):
132121 assert len (calls_info) == 1
133122 call_info = calls_info[0 ]
134123 assert call_info[" json" ] == {" text" : " text" }
124+ ```
135125
126+ #### dynamic sync mock
136127
137- # example of dynamic sync mock
138-
128+ ``` python
129+ import pytest
130+ from http import HTTPStatus
131+ from async_pytest_httpserver import (
132+ MockData,
133+ )
134+ from aiohttp.web import json_response, Request, Response
139135
140136def sync_mock_handler (request : Request) -> Response:
141137 return json_response(
142- {" result" : " some_result" },
143- status = HTTPStatus.OK ,
144- )
138+ {" result" : " some_result" },
139+ status = HTTPStatus.OK ,
140+ )
141+
145142
146143@pytest.mark.asyncio
147- async def test_sync_handler (client , some_service_mock_api ):
144+ async def test_sync_handler (client , some_service_mock ):
148145 # Arrange
149- calls_info = some_service_mock_api(sync_mock_handler)
146+ calls_info = some_service_mock(
147+ MockData(" POST" , " /some_api" , sync_mock_handler)
148+ )
150149
151150 # Act
152151 response = await client.post(
0 commit comments