I have a newbie question about mock patching async object with Python 3.8. All I am trying is to mock post() so that I can test the error handling logic, but so far I have had no luck. Could you please tell me what I did wrong? Thanks a lot.
main.py
import httpx
test_data = {"id": "123"}
class DebugClass:
def post(self):
try:
async with httpx.AsyncClient() as client:
res = await client.post(url='http://localhost:8080', data=test_data)
return "OK"
except Exception as e:
return "NOK"
test_main.py
from unittest import mock
from unittest.mock import patch, AsyncMock
from main import DebugClass
class TestClass:
@mock.patch('httpx.AsyncClient.post', new_callable=AsyncMock)
def test_post(self, mock_client_post):
mock_client_post = AsyncMock(side_effect=TimeoutError)
debug_class = DebugClass()
res = debug_class.post()
assert res == "NOK"