[TIP] Using Mock to patch an iterator / generator?
Herman Sheremetyev
herman at swebpage.com
Thu Jun 9 19:11:39 PDT 2011
Not sure about Mock but this is pretty simple with Flexmock:
http://has207.github.com/flexmock/user-guide.html#create-a-mock-generator
In your case that would be:
flexmock(Foo).should_receive('iter').and_yield(*self.partitions)
Cheers,
-Herman
On Fri, Jun 10, 2011 at 8:08 AM, Andrew Hammond
<andrew.george.hammond at gmail.com> wrote:
> I have a class that looks like
> class Foo(object):
> def iter(descending=False):
> for r in something_iterable:
> yield r
>
> I would like to mock the class and patch Foo.iter. I have tried a number of
> things without luck.
> class TestFoo(PatchedTestCase):
> _partitions = []
> def partitions(self):
> for p in self._partitions:
> yield p
> def postSetUpPreRun(self):
> self._partitions = [ 1, 2, 3, 4 ]
> rw = Mock(spec=Foo)
> self.mock_RollingWindow = rw
> rw.iter = Mock()
> rw.iter.side_effect = self.partitions
> 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?
> Andrew
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>
>
More information about the testing-in-python
mailing list