简体   繁体   中英

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"

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM