<div dir="ltr"><div>If I understand correctly, what you want is a mock object that is iterable. You can accomplish this using a MagicMock and setting the return value for `__iter__`. For example:</div><div><br></div><div><br>
</div><div>    from mock import MagicMock</div><div><br></div><div>    cur = MagicMock()</div><div>    cur.__iter__.return_value = [&#39;result 1&#39;, &#39;result 2&#39;]</div><div><br></div><div><br></div><div>Now, in your code under test, you can use the mock object to record the function call and also iterate over the results:</div>
<div><br></div><div><br></div><div>    cur.execute(&#39;SELECT * FROM test;&#39;)</div><div>    for record in cur:</div><div>        print record</div><div>    </div><div><br></div><div>This code will print:</div><div><br>
</div><div><br></div><div>    result 1</div><div>    result 2</div><div><br></div><div><br></div><div>And you can still make any desired assertions about the recorded calls:</div><div><br></div><div><br></div><div>    cur.execute.assert_called_once_with(&#39;SELECT * FROM test;&#39;)</div>
<div><br></div><div><br></div><div>Hope this helps,</div><div><br></div><div>Patrick Smith</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Mar 13, 2014 at 12:29 PM, Alexander Bandukwala <span dir="ltr">&lt;<a href="mailto:7h3kk1d@gmail.com" target="_blank">7h3kk1d@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I&#39;m trying to mock access to my postgres database using the mock framework. I saw the cursor example <a href="http://mock.readthedocs.org/en/latest/getting-started.html?highlight=cursor#setting-return-values-and-attributes" target="_blank">here</a>. The psycopg2 cursor however does not return the data set but is iterated over example <span></span><span></span><a href="http://initd.org/psycopg/docs/cursor.html" target="_blank">here</a>
 under result retrieval methods. What would be the best way to mock a 
cursor which can be called with a function but also be iterated over.<br>

<br>Thanks for any assistance and sorry for the inexperienced question.<span class="HOEnZb"><font color="#888888"><br clear="all"><br>-- <br><div dir="ltr">Alexander Bandukwala<div><span title="Call with Google Voice"><a href="tel:337-335-0133" value="+13373350133" target="_blank">337-335-0133</a></span></div>
<div>Twitter: <a href="http://twitter.com/abanduk" target="_blank">http://twitter.com/abanduk</a></div>

<div>Site: <a href="http://bandukwala.me/" target="_blank">http://bandukwala.me</a></div><div><br></div></div>
</font></span></div>
<br>_______________________________________________<br>
testing-in-python mailing list<br>
<a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
<br></blockquote></div><br></div>