如何模拟从一个本地协程到另一个使用的异步调用unittest.mock.patch?
我目前有一个很尴尬的解决方案:
class CoroutineMock(MagicMock):
def __await__(self, *args, **kwargs):
future = Future()
future.set_result(self)
result = yield from future
return result
Run Code Online (Sandbox Code Playgroud)
然后
class TestCoroutines(TestCase):
@patch('some.path', new_callable=CoroutineMock)
def test(self, mock):
some_action()
mock.assert_called_with(1,2,3)
Run Code Online (Sandbox Code Playgroud)
这有效,但看起来很难看.是否有更多的pythonic方式来做到这一点?