msg187692 - (view) |
Author: Michael Foord (michael.foord) *  |
Date: 2013-04-24 10:00 |
>>> from unittest.mock import create_autospec >>> def f(): pass ... >>> m = create_autospec(f) >>> m.side_effect = [1, 2] >>> m() Traceback (most recent call last): File "", line 1, in File "", line 3, in f File "/compile/py3k-cpython/Lib/unittest/mock.py", line 872, in __call__ return _mock_self._mock_call(*args, **kwargs) File "/compile/py3k-cpython/Lib/unittest/mock.py", line 931, in _mock_call result = next(effect) TypeError: 'list' object is not an iterator |
|
|
msg187840 - (view) |
Author: Michael Foord (michael.foord) *  |
Date: 2013-04-26 11:35 |
This illustrates the difference: >>> from mock import Mock, create_autospec >>> some_list = [1, 2, 3] >>> m = Mock() >>> m.side_effect = some_list >>> m.side_effect <listiterator object at 0x1004ab7d0> >>> m2 = create_autospec(lambda: None) >>> m2.side_effect = some_list >>> m2.side_effect [1, 2, 3] When setting a side_effect on a function (created by create_autospec) the side_effect setter is not used - so turning the list into an iterator needs to be done on first use instead. |
|
|
msg187844 - (view) |
Author: Kushal Das (kushal.das) *  |
Date: 2013-04-26 12:40 |
Working on this. |
|
|
msg187845 - (view) |
Author: Kushal Das (kushal.das) *  |
Date: 2013-04-26 12:45 |
Patch along with a test for the same. |
|
|
msg188036 - (view) |
Author: Kushal Das (kushal.das) *  |
Date: 2013-04-29 05:06 |
Version 2 of the patch, with typo fixed also one more addition test to check callable side effect in create_autospec. |
|
|
msg215916 - (view) |
Author: Éric Araujo (eric.araujo) *  |
Date: 2014-04-11 06:17 |
Michael, a patch including tests is ready for this issue. |
|
|
msg216098 - (view) |
Author: Michael Foord (michael.foord) *  |
Date: 2014-04-14 15:39 |
Can you explain why we need to check for the call_count here? I don't understand why this is needed. |
|
|
msg216164 - (view) |
Author: Kushal Das (kushal.das) *  |
Date: 2014-04-14 18:38 |
New patch with fix in proper place for side_effect for functions (includes test case). |
|
|
msg216196 - (view) |
Author: Roundup Robot (python-dev)  |
Date: 2014-04-14 20:10 |
New changeset 1e3c64470629 by Michael Foord in branch '3.4': Issue 17826. Setting an iterable side_effect on a mock created by create_autospec now works http://hg.python.org/cpython/rev/1e3c64470629 |
|
|