Mocking __iter__ with a magic mock in Python

Posted on Thu 29 March 2012 in Coding • Tagged with iteration, list comprehension, magic mock, Python

I just ran into a tiny little problem and thought I’d share the solution.

I was writing some tests in which I was mocking a dependency using Mock. My test code was roughly equivalent to the following (based on an example in the Mock documentation on magic methods):

>>> sub1 ...

Continue reading