【发布时间】:2015-09-09 12:54:11
【问题描述】:
如何使用 unittest.mock.patch 模拟从一个本机协程到另一个本机协程的异步调用?
我目前有一个相当尴尬的解决方案:
class CoroutineMock(MagicMock):
def __await__(self, *args, **kwargs):
future = Future()
future.set_result(self)
result = yield from future
return result
然后
class TestCoroutines(TestCase):
@patch('some.path', new_callable=CoroutineMock)
def test(self, mock):
some_action()
mock.assert_called_with(1,2,3)
这可行,但看起来很难看。有没有更多的pythonic方式来做到这一点?
【问题讨论】:
-
此外,由于 asyncio.tasks.ensure_future,此模拟不适用于 asyncio.await
标签: python python-asyncio python-mock