derwiki-adroll / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

PropertyMock refuses to raise AttributeErrror as a side effect #219

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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

Original issue reported on code.google.com by ahamme...@gmail.com on 26 Nov 2013 at 6:20