unit testing - Python mock method call arguments display the last state of a list -
I have a function that takes a list as a parameter function is called multiple times and every time something List prices are updated. I use the counterfeit object to capture call logic, always shows the latest value in the list for all call logic. The problem appears in the following code. MagicMock def multiple_calls_test (): From mock imports: m = MagicMock () Parameters = [xrange (= 0, 'some_fixed_value', 'some_fixed_value'], 10) for i = Parameters [0] = Arguments for MRX in arg.cs_larg_list: print args [0] [0] multiple_calls_test ()
And here is the output, notice the first list of all calls 9 Element is in the form of. [9, 'some_fixed_value'] [9, 'some_fixed_value'] [9, 'some_fixed_value'] [9, 'some_fixed_value', 'some_fixed_value [9,' some_fixed_value ',' some_fixed_value '] [9,' some_fixed_value ',' What some way to forge a counterfeit object to make a copy of the list argument instead of holding the reference, some_fixed_value '] [9,' some_fixed_value ',' some_fixed_value '] [9,' some_fixed_value ',' some_fixed_value '] [9] ? In some references in the actual list? Or is there any other way of emphasizing the right value for every method execution? Thank you.
Unfortunately, this looks a lack of Then (to state the obvious) just copy your code with a Please note that this has been tested only for the case used, so it can not be completed Robust solution for Msya, but hopefully it proves useful. duplicate library, and the code Seeing this fake library is not possible without patching, however, it seems that the effect you are looking for is a very light way to do this:
Fake Copy of import from import MagicMock class CopyArgsMagicMock (MagicMock): "Overlays" MagicMock so that we can store the references of the original logic objects. Store duplicate objects with copies of the arguments passed in the call. "" Diff _maccol (_mock_self, * args, ** kwargs): args_copy = copy.deepcopy (args) kwargs_copy = copy.deepcopy (kwargs) Return super (CopyArgsMagicMock , Itself). _ Mock_call (* args_copy, ** kwargs_copy)
MagicMock CopyArgsMagicMock and you should see the necessary behavior.
Comments
Post a Comment