[TIP] 'Mock' object is not iterable
Andrea Crotti
andrea.crotti.0 at gmail.com
Mon Mar 26 06:45:45 PDT 2012
On 03/26/2012 02:22 PM, Michael Foord wrote:
> On 26/03/2012 14:04, Andrea Crotti wrote:
>> I'm trying to mock out a function which looks for orphaned pyc files
>> in the filesystem.
>>
>> So I created something like this
>>
>> def fmock_b():
>> ret = Mock()
>> ret.pesky_pyc.return_value = ['b.pyc']
>> return ret
>>
>> and the function I'm testing does this:
>> for pyc in Walker(directory).pesky_pyc():
>>
>> but I get the error TypeError: 'Mock' object is not iterable
>>
>> But why?
>>
>> Actually also the non mocked version of Walker doesn't return a an
>> iterable from __init__, it's
>> pesky_pyc that returns an iterable, so why does it complain?
>>
>> Any hint about how to fix it?
>
> Hard to tell, but I suspect you want this (assuming "ret" is used to
> replace Walker):
>
> ret = Mock()
> mock_walker_instance = ret.return_value
> mock_walker_instance.pesky_pyc.return_value = ['b.pyc']
>
>
Thanks a lot that works.
Just out of curiosity, wouldn't also my version work if Mock.__init__
was returning self?
Wouldn't it make sense?
More information about the testing-in-python
mailing list