3

我有一门课,它做类似的事情:

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

如何模拟委托调用?

4

1 回答 1

2

哈克,但尝试:

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

然后在测试中:

my_mock = self.mox.CreateMock(MyMock)
my_mock.f().AndReturn(None)
于 2012-06-04T19:12:17.837 回答