[TIP] 'Mock' object is not iterable
Michael Foord
michael at voidspace.org.uk
Mon Mar 26 07:51:14 PDT 2012
On 26/03/2012 14:52, Andrea Crotti wrote:
> On 03/26/2012 02:47 PM, Michael Foord wrote:
>> __init__ methods can't return anything. Try it :-)
>>
>>
>> (Maybe you meant something else though? You could set
>> "ret.return_value = ret".)
>>
>
> Yes sorry I meant __call__ probably.
>
> I think the following should/might work:
>
> m = Mock()
> m.return_iterator.return_value = [1,2,3]
>
> and then
>
> for x in in m().return_iterator():
> ..
>
> and supposing m() return self it should work, right?
> I was looking at _mock_call to understand what it does and it returns
> something, but it's a bit
> too complicated to understand what exactly for me now..
>
Yes, but you shouldn't need to look at _mock_call at all - if you want a
mock to return itself when called just assign itself as its return value:
>>> from mock import Mock
>>> m = Mock()
>>> m.return_value = m
>>> m() is m
True
Michael
--
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