【发布时间】:2015-07-05 01:11:51
【问题描述】:
以下代码因ImBeingTested.i_call_other_coroutines 中的TypeError: 'Mock' object is not iterable 而失败,因为我已将ImGoingToBeMocked 替换为Mock 对象。
如何模拟协程?
class ImGoingToBeMocked:
@asyncio.coroutine
def yeah_im_not_going_to_run(self):
yield from asyncio.sleep(1)
return "sup"
class ImBeingTested:
def __init__(self, hidude):
self.hidude = hidude
@asyncio.coroutine
def i_call_other_coroutines(self):
return (yield from self.hidude.yeah_im_not_going_to_run())
class TestImBeingTested(unittest.TestCase):
def test_i_call_other_coroutines(self):
mocked = Mock(ImGoingToBeMocked)
ibt = ImBeingTested(mocked)
ret = asyncio.get_event_loop().run_until_complete(ibt.i_call_other_coroutines())
【问题讨论】:
标签: python python-3.x unit-testing mocking python-asyncio