jsatt / mock

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

Assigning mock property as another mock fails? #236

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
import mock

test_dict = {
    'test': 'abracadabra'
}
a = mock.Mock()
a.test = test_dict['test']
a.__dict__ = test_dict

b = mock.Mock()
b.foobar = a

yields:
Traceback (most recent call last):
  File "test.py", line 12, in <module>
    b.foobar = a
  File "/usr/local/lib/python2.7/site-packages/mock.py", line 783, in __setattr__
    if _check_and_set_parent(self, value, name, name):
  File "/usr/local/lib/python2.7/site-packages/mock.py", line 434, in _check_and_set_parent
    if ((value._mock_name or value._mock_new_name) or
  File "/usr/local/lib/python2.7/site-packages/mock.py", line 656, in __getattr__
    elif self._mock_methods is not None:
  File "/usr/local/lib/python2.7/site-packages/mock.py", line 655, in __getattr__
    raise AttributeError(name)
AttributeError: _mock_methods

Why does this fail?

Original issue reported on code.google.com by brett.tr...@workiva.com on 15 Aug 2014 at 5:05

GoogleCodeExporter commented 8 years ago
Python 2.7.7
mock 1.0.1

Original comment by brett.tr...@workiva.com on 15 Aug 2014 at 5:09

GoogleCodeExporter commented 8 years ago
Because __dict__ on a mock object can't be replaced like that! If you don't 
replace __dict__ it works fine.

Original comment by fuzzyman on 15 Aug 2014 at 6:32

GoogleCodeExporter commented 8 years ago
The odd thing is "a" works fine as is, but its not until I try to assign an 
arbitrary property on a different mock that it gets angry.

Original comment by brett.tr...@workiva.com on 15 Aug 2014 at 3:19