Export to GitHub

mock - issue #219

PropertyMock refuses to raise AttributeErrror as a side effect


Posted on Nov 26, 2013 by Swift Camel

What steps will reproduce the problem?

>>> import mock >>> a_mock = mock.MagicMock() >>> no_attribute = mock.PropertyMock(side_effect=AttributeError) >>> type(a_mock).property = no_attribute

What is the expected output? What do you see instead?

I would expect the above to raise an AttributeError. Instead it returns a MagicMock instance.

>>> a_mock.property <MagicMock name='mock.property' id='140165240345424'>

I would expect it to have the same effect as calling a PropertyMock with any other exception as a side effect:

>>> mock_value_error = mock.PropertyMock(side_effect=ValueError) >>> type(a_mock).other_property = mock_value_error >>> a_mock.other_property Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/ahammel/bin/python/mock-1.0.1-py2.6.egg/mock.py", line 2365, in get return self() File "/home/ahammel/bin/python/mock-1.0.1-py2.6.egg/mock.py", line 955, in call return _mock_self._mock_call(*args, **kwargs) File "/home/ahammel/bin/python/mock-1.0.1-py2.6.egg/mock.py", line 1010, in _mock_call raise effect ValueError

What version of the product are you using? On what operating system?

Using version mock-1.0.1-py2.6 on CentOS 6.4

Please provide any additional information below.

PropertyMock objects apparently won't raise sublcasses of AttributeError either:

>>> class MockAttributeError(AttributeError): pass ... >>> no_attr = mock.PropertyMock(side_effect=MockAttributeError) >>> type(a_mock).property = no_attr >>> a_mock.property <MagicMock name='mock.property' id='140165240345424'>

Works fine for subclasses of other Exceptions:

>>> class MockKeyError(KeyError): pass ... >>> no_key = mock.PropertyMock(side_effect=MockKeyError) >>> type(a_mock).property = no_key >>> a_mock.property Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/ahammel/bin/python/mock-1.0.1-py2.6.egg/mock.py", line 2365, in get return self() File "/home/ahammel/bin/python/mock-1.0.1-py2.6.egg/mock.py", line 955, in call return _mock_self._mock_call(*args, **kwargs) File "/home/ahammel/bin/python/mock-1.0.1-py2.6.egg/mock.py", line 1010, in _mock_call raise effect main.MockKeyError

It's apparently not just me:

http://stackoverflow.com/questions/18236123/python-porpertymock-side-effect-with-attributeerror-and-valueerror

Status: New

Labels:
Type-Defect Priority-Medium