從this question繼,我試圖修補A()
類Mock()
這樣,當B()
被初始化,素被用作基本如:單元測試:修補的對象__base__
class A(object): ...
class B(A): ...
def setUp(self):
with patch('A', new_callable=Mock) as MockObject:
self.b = B()
self.b.__class__.__base__ = MockOjbect
不工作因爲基地是隻讀的。做這件事的正確方法是什麼?
更新:
>>> from mock import Mock
>>> class A(object):
... pass
...
>>> class B(A):
... pass
...
>>> b.__class__.__bases__ = (Mock,)
>>> b.__class__.__bases__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/envs/myenv/local/lib/python2.7/site-packages/mock.py", line 656, in __getattr__
elif self._mock_methods is not None:
File "/opt/envs/myenv/local/lib/python2.7/site-packages/mock.py", line 655, in __getattr__
raise AttributeError(name)
AttributeError: _mock_methods
要清楚,我不相信這是要做到我想做的事情,我希望一半別人會拿出另一種方式的最佳途徑。
您的原始問題是:「我如何修補對象的__bases__?」。我不確定你想要做什麼,並相信我的回答現在對你的跟進無效。你想達到什麼目的? –