<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
On 11/06/2011 10:35, Michael Foord wrote:
<blockquote cite="mid:4DF336D2.8030208@voidspace.org.uk" type="cite">
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
On 10/06/2011 00:08, Andrew Hammond wrote:
<blockquote
cite="mid:BANLkTi=7+x_etwhMhafk-0mbqc8wNiMs6A@mail.gmail.com"
type="cite">I have a class that looks like
<div><br>
</div>
<div>class Foo(object):</div>
<div> def iter(descending=False):</div>
<div> for r in something_iterable:</div>
<div> yield r</div>
<div><br>
</div>
<div><br>
</div>
</blockquote>
Hey Andrew,<br>
<br>
Because of the use of yield, your iter is a generator and not a
normal method. Mocking a generator (or any iterable) can be done
with the MagicMock (in mock 0.7).<br>
<br>
You can then configure the return values using something similar
to this:<br>
<br>
mock = MagicMock()<br>
<br>
values = [1, 2, 3]<br>
mock.iter.__iter__.side_effect = lambda: values.pop()<br>
<br>
</blockquote>
<br>
Actually, this isn't quite correct (my apologies).<br>
<br>
Foo.iter *returns* a generator when called. It isn't directly a
generator. Also the __iter__ method should return the iterator.<br>
<br>
>>> values = [1, 2, 3]<br>
>>> mock_foo = MagicMock()<br>
>>> mock_foo.iter.return_value.__iter__.return_value =
iter(values)<br>
>>> list(mock_foo.iter())<br>
[1, 2, 3]<br>
<br>
I'm adding this as a recipe to the "examples" page as I got it wrong
on the first attempt... :-)<br>
<br>
Michael<br>
<blockquote cite="mid:4DF336D2.8030208@voidspace.org.uk" type="cite">
All the best,<br>
<br>
Michael Foord<br>
<br>
<blockquote
cite="mid:BANLkTi=7+x_etwhMhafk-0mbqc8wNiMs6A@mail.gmail.com"
type="cite">
<div>I would like to mock the class and patch Foo.iter. I have
tried a number of things without luck. </div>
<div><br>
</div>
<div>
<div>class TestFoo(PatchedTestCase):</div>
<div> _partitions = []</div>
<div><br>
</div>
<div> def partitions(self):</div>
<div> for p in self._partitions:</div>
<div> yield p</div>
<div><br>
</div>
<div> def postSetUpPreRun(self):</div>
<div> self._partitions = [ 1, 2, 3, 4 ]</div>
<div> rw = Mock(spec=Foo)</div>
<div> self.mock_RollingWindow = rw</div>
<div> rw.iter = Mock()</div>
<div> rw.iter.side_effect = self.partitions</div>
<div><br>
</div>
</div>
<div>This is following the pattern I've used for other methods,
but doesn't work for an iterator... so, what is the proper
approach please?</div>
<div><br>
</div>
<div>Andrew</div>
<pre wrap=""><fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
testing-in-python mailing list
<a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a>
<a moz-do-not-send="true" class="moz-txt-link-freetext" href="http://lists.idyll.org/listinfo/testing-in-python">http://lists.idyll.org/listinfo/testing-in-python</a>
</pre>
</blockquote>
<br>
<br>
<pre class="moz-signature" cols="72">--
<a moz-do-not-send="true" class="moz-txt-link-freetext" href="http://www.voidspace.org.uk/">http://www.voidspace.org.uk/</a>
May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing <a moz-do-not-send="true" class="moz-txt-link-freetext" href="http://www.sqlite.org/different.html">http://www.sqlite.org/different.html</a>
</pre>
<pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
testing-in-python mailing list
<a class="moz-txt-link-abbreviated" href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a>
<a class="moz-txt-link-freetext" href="http://lists.idyll.org/listinfo/testing-in-python">http://lists.idyll.org/listinfo/testing-in-python</a>
</pre>
</blockquote>
<br>
<br>
<pre class="moz-signature" cols="72">--
<a class="moz-txt-link-freetext" href="http://www.voidspace.org.uk/">http://www.voidspace.org.uk/</a>
May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing <a class="moz-txt-link-freetext" href="http://www.sqlite.org/different.html">http://www.sqlite.org/different.html</a>
</pre>
</body>
</html>