I have a function that takes a list as a parameter. The function is called several times and every time some of the list values are updated. The moral object that I use to capture call arguments always shows the last values in the list for all call arguments. The following code shows the problem.
from mock import MagicMock
def multiple_calls_test():
m = MagicMock()
params = [0, 'some_fixed_value', 'some_fixed_value']
for i in xrange(1,10):
params[0] = i
m(params)
for args in m.call_args_list:
print args[0][0]
multiple_calls_test()
And here is the result: Note that all calls have 9 as the first element of the list.
[9, 'some_fixed_value', 'some_fixed_value']
[9, 'some_fixed_value', 'some_fixed_value']
[9, 'some_fixed_value', 'some_fixed_value']
[9, 'some_fixed_value', 'some_fixed_value']
[9, 'some_fixed_value', 'some_fixed_value']
[9, 'some_fixed_value', 'some_fixed_value']
[9, 'some_fixed_value', 'some_fixed_value']
[9, 'some_fixed_value', 'some_fixed_value']
[9, 'some_fixed_value', 'some_fixed_value']
Is there a way to force the mock to make a copy of the list argument instead of referencing the actual list? Or is there any other way to assert the correct value for each method execution? Thank.
Babar