我有一个测试如下:
import mock
# other test code, test suite class declaration here
@mock.patch("other_file.another_method")
@mock.patch("other_file.open", new=mock.mock_open(read=["First line", "Second line"])
def test_file_open_and_read(self, mock_open_method, mock_another_method):
self.assertTrue(True) # Various assertions.
我得到以下错误:
TypeError: test_file_open_and_read() takes exactly 3 arguments (2 given)
我试图指定我希望用mock.mock_open
而不是mock.MagicMock
来模拟另一个文件的__builtin__.open
方法,这是patch
装饰器的默认行为。我该怎么做?在
应该使用
new_callable
而不是new
。也就是说注意,我们将函数句柄}文档中的示例所示。在
mock.mock_open
传递给new_callable
,而不是结果对象。这允许我们执行mock_open_method().write
来访问write
函数,就像{您错过了来自
open
内建的参数create
。在相关问题 更多 >
编程相关推荐