0

I have a list of expected arguments, I need to check if the given function is called with all elements of the list. I came up with this. I know looping is better way.

expected_list = [(1,2,3),(4,5,6)]
real_writer = mock.Mock()
real_writer.write.assert_any_call(expected_list[0])
real_writer.write.assert_any_call(expected_list[1])

Is there any way where I can pass the list itself??

Ishan Bhatt
  • 9,287
  • 6
  • 23
  • 44
  • Possible duplicate of [Checking whether function has been called multiple times with different parameters](http://stackoverflow.com/questions/34225688/checking-whether-function-has-been-called-multiple-times-with-different-paramete) – Michele d'Amico Jan 08 '16 at 11:55

1 Answers1

0

You could do it like this:

expected_list = [(1,2,3),(4,5,6)]
real_writer = mock.Mock()
real_writer.write.assert_has_calls((mock.call(exp) for exp in expected_list), any_order=True)

If you actually expect the calls to be in the order they are in the list you can omit the any_order argument.

Turn
  • 6,656
  • 32
  • 41
  • In the OP context is clear that he `any_order=False` to mimic his code. You should point out that `assert_has_calls` match even in the case that there are more calls. – Michele d'Amico Jan 08 '16 at 11:53
  • @Micheled'Amico How is it clear that `any_order=False` is what he needs? It doesn't matter what order they call `assert_any` in. Also, I can make a note about the semantics of `assert_any` but I assume the OP understood this already. My code mimics what theirs does in this respect as well. – Turn Jan 08 '16 at 16:51