Tuesday 23 November 2021

Python - object MagicMock can't be used in 'await' expression

 You can get mocks to return objects that can be awaited by using a Future. The following is a pytest test case, but something similar should be possible with unittest.

async def test_that_mock_can_be_awaited():
    mock = MagicMock(return_value=Future())
    mock.return_value.set_result(123)
    result = await mock()
    assert result == 123

In your case, since you're patching Service (which gets passed in as mock), mock.return_value = Future() should do the trick.


from: https://stackoverflow.com/questions/51394411/python-object-magicmock-cant-be-used-in-await-expression

No comments:

Post a Comment