如何模拟从getattr返回的函数?



我有一个类可以做这样的事情:

class MyClass(object):
    def __init__(self, delegate_to):
        self._delegate_to = delegate_to
    def __getattr__(self, item):
        return getattr(self._delegate_to, item)

但是当我尝试做这样的事情时:

my_mock = self.mox.CreateMock(MyClass)
my_mock.f().AndReturn(None)

MOX 错误与:

UnknownMethodCallError: Method called is not a member of the object: f

如何模拟委派呼叫?

Hacky,但请尝试:

class MyMock(MyClass):
    def f():
        pass

然后在测试中:

my_mock = self.mox.CreateMock(MyMock)
my_mock.f().AndReturn(None)

相关内容

  • 没有找到相关文章