在 Python 中,我想__init__
使用 minimock 库来模拟一个类的方法。
这就是解释器所做的(ipython):
In [1]: import minimock
In [2]: class A:
...: def __init__(self):
...: print "REAL INIT"
...:
In [3]: def new_init(self):
...: print "NEW INIT"
...:
In [4]: minimock.mock("A.__init__", returns_func=new_init)
In [5]: a = A()
Called A.__init__()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-144b248f218a> in <module>()
----> 1 a = A()
D:\Tools\Python27\lib\site-packages\minimock\__init__.pyc in __call__(self, *args, **kw)
492 if self.mock_tracker is not None:
493 self.mock_tracker.call(self.mock_name, *args, **kw)
--> 494 return self._mock_return(*args, **kw)
495
496 def _mock_return(self, *args, **kw):
D:\Tools\Python27\lib\site-packages\minimock\__init__.pyc in _mock_return(self, *args, **kw)
505 raise Exception("No more mock return values are present.")
506 elif self.mock_returns_func is not None:
--> 507 return self.mock_returns_func(*args, **kw)
508 else:
509 return None
TypeError: new_init() takes exactly 1 argument (0 given)
但是,如果我从 new_init 中删除 self 参数:
def new_init():
print "NEW INIT"
实例化 A 类给出:
In [13]: a = A()
Called A.__init__()
NEW INIT
这让我认为 minimock 对“自我”有限制。
您知道是否可以在传递给 minimock 的方法的模拟版本中使用“self”?