Reset a python mock generator return value
<p>How can I "reset" a method that returns a generator. If I mock this method but use the parent class twice in a method under test, the first call consumes the generator and the second call has no data. Sample code below. The two calls to get_values should return the same (mocked) list.</p>

<pre><code>import mock

class MyTestClass(object):
    def __init__(self, param):
        self.param = param

    def get_values(self):
        return self.param


class MyTestRunner(object):
    def __init__(self):
        pass

    def run(self):
        cls = MyTestClass(2)
        print list(cls.get_values())
        cls = MyTestClass(3)
        print  list(cls.get_values())


with mock.patch.object(MyTestClass, 'get_values') as mock_class:
    mock_class.return_value = ({'a': '10', 'b': '20'}).iteritems()
    m = MyTestRunner()
    m.run()
</code></pre>

<p>Expected:</p>

<pre><code>[('a', '10'), ('b', '20')]
[('a', '10'), ('b', '20')]
</code></pre>

<p>Actual:</p>

<pre><code>[('a', '10'), ('b', '20')]
[]
</code></pre>

<p>How's this?</p>

<pre><code>mock_class.side_effect = lambda x: {'a': '10', 'b': '20'}.iteritems()
</code></pre>

<p>Side effect occurs every call thus recreates every time.</p>

<p>You can even set the dict before like so</p>

<pre><code>my_dict = {'a': '10', 'b': '20'}
mock_class.side_effect = lambda x: my_dict.iteritems()
</code></pre>

<p>The return value of side_effect is the result of the call.</p>

python mocking