我的测试如下:
FCK.CreateLink=function(A){FCK.ExecuteNamedCommand('Unlink');if (A.length>0){var B='javascript:void(0);/*'+(new Date().getTime())+'*/';FCK.ExecuteNamedCommand('CreateLink',B);var C=this.EditorDocument.evaluate("//a[@href='"+B+"']",this.EditorDocument.body,null,9,null).singleNodeValue;if (C){C.href=A;return C;}}};
我收到以下错误:
TypeError:test_file_open_and_read()只需要3个参数(给定2个)
我正在尝试指定我希望使用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.
而不是__builtin__.open
来模拟其他文件的mock.mock_open
方法,这是mock.MagicMock
装饰器的默认行为。我怎么能这样做?
答案 0 :(得分:2)
您错过了create
内置的参数open
。
@mock.patch("other_file.open", new=mock.mock_open(read=["First line", "Second line"], create=True)
答案 1 :(得分:1)
应使用new_callable
代替new
。也就是说,
@mock.patch("other_file.open", new_callable=mock.mock_open)
def test_file_open_and_read(self, mock_open_method):
# assert on the number of times open().write was called.
self.assertEqual(mock_open_method().write.call_count,
num_write_was_called)
请注意,我们将函数句柄mock.mock_open
传递给new_callable
,而不是结果对象。这样我们就可以mock_open_method().write
访问write
函数,就像mock_open
节目文档中的示例一样。