Houman
Houman

Reputation: 66410

How to mock a method within an async unit test?

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"

Upvotes: 7

Views: 7090

Answers (1)

Houman
Houman

Reputation: 66410

It's actually much simpler than I thought, a normal @patch from from unittest.mock import patch is sufficient. It recognises the async methods and injects an AsyncMock automatically.

@pytest.mark.asyncio
@patch("service.auth_service.AuthService.generate_token")
async def test_successful_register_returns_device_token(self, mock_token):
        mock_token.return_value = "321"
        async with AsyncClient(app=app, base_url="http://testserver") as ac:
            response = await ac.post(
                "/register/",
                headers={},
                json={},
            )
            assert response.status_code == 201
            assert "device_token" in response.json()
            assert response.json()["device_token"] == "321"

Upvotes: 8

Related Questions