我想模拟某个类派生的对象,我尝试下面的代码,但运行时错误为maximum recursion depth exceeded
。我想我没有以合理的方式修补属性获取器,导致死循环。
如何仅模拟该类的给定属性?
类A.__getattribute__
与对象a.__getattribute__
之间有什么区别吗?
class A(object):
def __init__(self, val):
self.val = val
def get_val(self):
return self.val
def test_A_val(mocker):
a = A(True)
assert a.get_val()
mocker.patch.object(A, 'get_val', lambda x: False)
mocker.patch(__name__ + '.A.__getattribute__', wraps=lambda x: False if x == 'val' else getattr(A, x))
a = A(True)
assert not a.get_val()
assert not a.val
assert a.get_val()