<!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 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>
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 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>