它实际上比我想象的要简单得多,一个来自
from unittest.mock import patch
@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"