Python 检查来自模拟对象的调用参数

Python 检查来自模拟对象的调用参数,python,unit-testing,mocking,Python,Unit Testing,Mocking,如果我更改了一个对象(用作模拟的参数),在调用此模拟后,mock.call中的对象也会更改。由于这句话非常令人困惑,让我告诉你我的意思: from unittest import mock test_mock = mock.Mock() test_parameter = ["only_entry_of_this_list"] test_mock(argument=test_parameter) test_mock.assert_has_calls([mock.call(

如果我更改了一个对象(用作模拟的参数),在调用此模拟后,
mock.call
中的对象也会更改。由于这句话非常令人困惑,让我告诉你我的意思:

from unittest import mock

test_mock = mock.Mock()

test_parameter = ["only_entry_of_this_list"]
test_mock(argument=test_parameter)

test_mock.assert_has_calls([mock.call(argument=["only_entry_of_this_list"])])
工作非常好,就像我预期的那样。但是,如果我在调用模拟后更改
test\u参数
,如下所示:

from unittest import mock

test_mock = mock.Mock()

test_parameter = ["only_entry_of_this_list"]
test_mock(argument=test_parameter)
test_parameter.append("another_entry_of_this_list")

test_mock.assert_has_calls([mock.call(argument=["only_entry_of_this_list"])])
我得到:

AssertionError: Calls not found.
Expected: [call(argument=['only_entry_of_this_list'])]
Actual: [call(argument=['only_entry_of_this_list', 'another_entry_of_this_list'])]

我现在的问题是,这是否是预期的行为,因为这将使测试具有迭代变化参数的模拟的迭代调用变得困难,因为只有参数的最新状态是可访问的。

不知道这是否是有意的。但是您可以在每次调用中尝试
test\u mock(argument=test\u parameter[:])
来解耦。不知道这是否是有意的。但是您可以在每次调用中尝试
test\u mock(argument=test\u parameter[:])
来解耦。