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 exception side_effect on a mock from create_autospec does not work #67849
Comments
The following fails on python 3.4.2, 3.4.3 and 3.5.0a2 (downloaded from python.org and compiled on Ubuntu 14.04). >>> from unittest.mock import create_autospec
>>> def function():
... pass
...
>>> mock = create_autospec(function)
>>> mock.side_effect = ValueError('MyError')
>>> mock()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 3, in function
File "/usr/local/lib/python3.5/unittest/mock.py", line 910, in __call__
return _mock_self._mock_call(*args, **kwargs)
File "/usr/local/lib/python3.5/unittest/mock.py", line 963, in _mock_call
effect = self.side_effect
File "/usr/local/lib/python3.5/unittest/mock.py", line 510, in __get_side_effect
sf = _MockIter(sf)
File "/usr/local/lib/python3.5/unittest/mock.py", line 351, in __init__
self.obj = iter(obj)
TypeError: 'ValueError' object is not iterable But, on Python 3.3.5, 3.4.0, or when the mock is created via Mock(), for instance, the exception is raised as expected: [...]
>>> mock()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 3, in function
File "/usr/lib/python3.4/unittest/mock.py", line 885, in __call__
return _mock_self._mock_call(*args, **kwargs)
File "/usr/lib/python3.4/unittest/mock.py", line 941, in _mock_call
raise effect
ValueError: MyError |
The problem only affects autospecced functions. Apparently, the problem lies here (all code excerpts from Lib/unittest/mock.py):
197 def _setup_func(funcopy, mock):
504 def __get_side_effect(self): I've attached a patch which adds a test for this use case, and a proposed fix. Hope it helps :) |
Also reported in the mock project as testing-cabal/mock#264 |
This looks fine to me, I'm going to apply it. |
New changeset db825807ab04 by Robert Collins in branch 'default': |
New changeset 7021d46c490e by Robert Collins in branch '3.5': New changeset 231bf0840f8f by Robert Collins in branch 'default': |
python 3.4.3 is also affected. Is it possible to fix this 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: