最佳答案
我在 Python 中使用 嘲笑,想知道这两种方法中哪一种更好(请阅读: more pythonic)。
方法一 : 创建一个模拟对象并使用它:
def test_one (self):
mock = Mock()
mock.method.return_value = True
self.sut.something(mock) # This should called mock.method and checks the result.
self.assertTrue(mock.method.called)
方法2 : 使用补丁创建一个 mock:
@patch("MyClass")
def test_two (self, mock):
instance = mock.return_value
instance.method.return_value = True
self.sut.something(instance) # This should called mock.method and checks the result.
self.assertTrue(instance.method.called)
这两种方法做同样的事情。我不确定的差异。
有人能告诉我吗?