我想在模拟 Base 时为 Subclass 编写测试,因为 Base 来自外部库。鉴于我们修改了基类中的所有可调用对象,我该如何模拟它。
class SubClass(Base):
def __init__(self, *args, **argv):
super().__init__(*args, **argv)
for attr_name in Base.__dict__:
attr = getattr(self, attr_name)
if callable(attr):
setattr(self, attr_name, functools.partial(__class__.sanity_check, attr))
@classmethod
def sanity_check(func):
txt = func()
if 'banned_word' in txt:
raise Exception('Device has banned word on it!')
return txt