0
類屬性如何被模擬?嘲弄的財產不在課堂上工作。屬性的Python mock.patch.object
代碼示例:
class Box(object):
def __init__(self, size):
self._size = size
@property
def size(self):
return self._size
def volume(self):
print(self.size)
return self.size**3
def get_new_size():
return 42
box = Box(13)
with mock.patch.object(Box, 'size', get_new_size):
print(box.volume())
返回:
<bound method Box.get_new_size of <__main__.Box object at 0x10a8b2cd0>>
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "<stdin>", line 9, in volume
TypeError: unsupported operand type(s) for ** or pow(): 'instancemethod' and 'int'