根据side_effect
文档,它应该是可调用的、可迭代的或异常(类或对象)。此外,我猜您想替换's Classmake
中的 unboundmethod wheel
:要进行此类修补,您应该使用patch
或patch.object
作为上下文或装饰器,如此处所述。
以下是如何做到这一点的完整示例:
from mock import patch
class Wheel():
def __init__(self):
self.rubber = 0
def make(self):
self.rubber = 10
soft = 36
def mockfunc(wheel):
wheel.rubber = soft
wheel = Wheel()
print("unpatched wheel before make rubber = {}".format(wheel.rubber))
wheel.make()
print("unpatched wheel after make rubber = {}".format(wheel.rubber))
with patch.object(Wheel, "make", side_effect=mockfunc, autospec=True):
print("patched wheel before make rubber = {}".format(wheel.rubber))
wheel.make()
print("patched wheel after make rubber = {}".format(wheel.rubber))
输出是
unpatched wheel before make rubber = 0
unpatched wheel after make rubber = 10
patched wheel before make rubber = 10
patched wheel after make rubber = 36