[TIP] list side_effect -> "list is not an iterator"
Michael Foord
michael at voidspace.org.uk
Wed Apr 24 03:12:27 PDT 2013
On 22 Apr 2013, at 14:10, Michael Foord <fuzzyman at voidspace.org.uk> wrote:
>
> On 19 Apr 2013, at 22:08, Chris Conway <clconway at google.com> wrote:
>
>> What am I doing wrong here?
>>
>>>>> import mock
>>>>> fn = mock.Mock()
>>>>> fn.side_effect = [1,2]
>>>>> fn(), fn()
>> (1, 2)
>>
>> but
>>
>>>>> fn = mock.create_autospec(lambda: None)
>>>>> fn.side_effect = [1,2]
>>>>> fn(), fn()
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> File "<string>", line 3, in funcopy
>> File "mock.py", line 974, in __call__
>> return _mock_self._mock_call(*args, **kwargs)
>> File "mock.py", line 1031, in _mock_call
>> result = next(effect)
>> TypeError: list object is not an iterator
>>
>> whereas
>>
>>>>> fn = mock.create_autospec(lambda: None, side_effect=[1,2])
>>>>> fn(), fn()
>> (1, 2)
>>
>
> I'm pretty sure this is a bug in mock. You can get round it with: fn.side_effect = iter([1, 2])
>
> That's obviously not ideal. I'm hoping to have a proper look at it tonight and I'll come back to you.
It's definitely a bug and setting the side_effect should work as you expect(ed). I've filed an issue for it.
http://bugs.python.org/issue17826
Should be a relatively easy fix for anyone who knows the mock code base. For anyone who doesn't it's an interesting opportunity to drop into the bowels of mock and provide a patch :-)
All the best,
Michael
>
> Thanks,
>
> Michael
>
>> _______________________________________________
>> 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
>
>
>
>
>
>
> _______________________________________________
> 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