0
我在模拟消息队列,我希望它返回2条消息,然后在每次调用时都返回None(模拟消息队列现在为空)。但是,在第三个消息调用之后,我从模拟框架中得到一个错误,说没有“下一个”值。Python模拟返回初始预定义值返回默认值
下面是一个例子:
from mock import MagicMock
mock_message_queue = MagicMock(name='message_queue')
expected_messages = ['message 1', 'message 2', None]
mock_message_queue.fetch.side_effect = expected_messages
print('mock_message_queue.fetch() = {}'.format(mock_message_queue.fetch()))
print('mock_message_queue.fetch() = {}'.format(mock_message_queue.fetch()))
print('mock_message_queue.fetch() = {}'.format(mock_message_queue.fetch()))
print('mock_message_queue.fetch() = {}'.format(mock_message_queue.fetch()))
输出:
mock_message_queue.fetch() = message 1
mock_message_queue.fetch() = message 2
mock_message_queue.fetch() = None
Traceback (most recent call last):
File "/home/wsi/.PyCharm2017.1/config/scratches/mock_message_bus.py", line 10, in <module>
print('mock_message_queue.fetch() = {}'.format(mock_message_queue.fetch()))
File "/usr/local/lib/python3.5/dist-packages/mock/mock.py", line 1062, in __call__
return _mock_self._mock_call(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/mock/mock.py", line 1121, in _mock_call
result = next(effect)
StopIteration
Process finished with exit code 1
所以基本上我想它总是返回无返回 '消息2' 之后。有人知道怎么做吗?