2012-06-04 53 views
1

我有一個類,確實是這樣的:如何模擬從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 

如何模擬出委派的電話?

回答

2

哈克,但嘗試:

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

然後在測試:

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