1

我的标题是相当描述性的,但这里有。假设我有这个设置。

class BaseClass(object):
    def __init__(self):
        pass
    def base_function(self, param="Hello World"):
        print param

#multiple inheritance probably irrelevant but my problem deals with it
class DerivedClass(BaseClass, AnotherBaseClass):
    def __init__(self):
        pass
    def advanced_function(self):
        #blah blah blah
        #code code code
        self.base_function()

现在,我有一种情况,我正在测试派生类,但在这样做时,我需要确保调用它的基类方法。我试着做这样的事情

from mock import MagicMock

d = DerivedClass()
super(DerivedClass, d).base_function = MagicMock()
d.advanced_function()
super(DerivedClass, d).base_function.assert_called()

我 100% 确定这个设置是错误的,因为

AttributeError: 'super' object has no attribute 'base_function'

我知道我在 super 上做错了什么,有人知道吗?

4

1 回答 1

1

通过 访问BaseClass.base_function。因为你不重载方法,你只是继承它,DerivedClass.base_function是同一个对象:

id(BaseClass.base_function) == id(DerivedClass.base_function)

创建实例时,它们会继承模拟。

于 2013-12-11T22:28:06.203 回答