擅长:python、mysql、java
<p>您可以使用<code>Mock</code>的<code>side_effect</code>参数来替代正在测试的类的<code>__next__</code>属性。在</p>
<p>根据<a href="https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock" rel="nofollow noreferrer">documentation</a>:</p>
<blockquote>
<p>If <code>side_effect</code> is an iterable then each call to the mock will return
the next value from the iterable.</p>
</blockquote>
<p>示例:</p>
<pre><code>from unittest.mock import Mock
class Iterable:
def __iter__(self):
return self
Iterable.__next__ = Mock(side_effect=[1, 2, 3])
for i in Iterable():
print(i)
</code></pre>
<p>该输出:</p>
^{pr2}$