-
-
Notifications
You must be signed in to change notification settings - Fork 29.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting a side_effect on mock from create_autospec doesn't work #62026
Comments
>>> 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 "<stdin>", line 1, in <module>
File "<string>", 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 |
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. |
Working on this. |
Patch along with a test for the same. |
Version 2 of the patch, with typo fixed also one more addition test to check callable side effect in create_autospec. |
Michael, a patch including tests is ready for this issue. |
Can you explain why we need to check for the call_count here? I don't understand why this is needed. |
New patch with fix in proper place for side_effect for functions (includes test case). |
New changeset 1e3c64470629 by Michael Foord in branch '3.4': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: