I have a class called database.py with a function called generate_token().
I would like to mock it and return a fixed value 321
. So that I can see that the method was called and the return value returned.
How do I mock that? This is what I have tried.
@pytest.mark.asyncio
async def test_successful_register_returns_device_token(monkeypatch):
async def mock_generate_token():
return "321"
m = AsyncMock(mock_generate_token)
m.return_value = "321"
async with AsyncClient(app=app, base_url="http://127.0.0.1") as ac:
monkeypatch.setattr(database, "generate_token", m)
response = await ac.post(
"/register/",
headers={},
json={},
)
assert response.status_code == 201
assert "device_token" in response.json()
assert response.json()["device_token"] == "321"