[TIP] 'Mock' object is not iterable

Michael Foord michael at voidspace.org.uk
Mon Mar 26 06:22:24 PDT 2012


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']

HTH,

Michael Foord



>
> Thanks,
> Andrea
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>


-- 
http://www.voidspace.org.uk/

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 http://www.sqlite.org/different.html




More information about the testing-in-python mailing list