【发布时间】:2015-08-17 13:07:23
【问题描述】:
我正在尝试模拟一个协程。因此,这个模拟的__next__() 和close() 被调用。虽然模拟 close() 有效,但我无法模拟 __next__():
mock_coroutine = mock.Mock()
mock_coroutine.some_call(1, 2, 3)
mock_coroutine.some_call.assert_called_with(1, 2, 3)
mock_coroutine.close()
mock_coroutine.close.assert_called_with()
#this fails
mock_coroutine.__next__()
mock_coroutine.__next__.assert_called_with()
我错过了什么?如何确保调用了我的 mock 的 __next__() 方法?
目前,我正在使用以下内容:
class MockCoroutine:
def __next__(self):
self.called_next = True
def close(self):
self.called_exit = True
但是,我更喜欢使用标准模拟。
【问题讨论】:
标签: python unit-testing python-3.x mocking