jeudi 25 août 2016

Python mock call with changing input

I'm trying to do a test on a method that does a repeated call to a mocked method, where one of the variables is a dictionary, and the value of one of the keys is changed between calls. Something like this:

my_method(...):
  # Do something
  input_dictionary= {...}
  ....
  input_dictionary['new_key'] = GENERATED_KEY_1
  mock_method(input_dictionary, othervariable1, ...)
  ......
  input_dictionary['new_key'] = GENERATED_KEY_2
  mock_method(input_dictionary, othervariable2, ...)

In my unit test of this method I check that the call is made with the exact values using assert_any_call ... which fails for the first case/first generated key. And when I look in the calls captured by the mock object I can see the 2 calls, but the first one is using the second key:

call({...'new_key': 'GENERATED_KEY_2'}, othervariable1, ...)
call({...'new_key': 'GENERATED_KEY_2'}, othervariable2, ...)

Any suggestions on how to solve this?

When stepping through the code I can see that the calls are made using the correct keys ... but it looks like mock doesn't capture them correctly/shares the parameters between the calls.

Aucun commentaire:

Enregistrer un commentaire