相关疑难解决方法(0)

在python 3.5中模拟异步调用

如何模拟从一个本地协程到另一个使用的异步调用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方式来做到这一点?

python python-mock python-asyncio

32
推荐指数
7
解决办法
2万
查看次数

标签 统计

python ×1

python-asyncio ×1

python-mock ×1