0

i would like to unpack the arguments of a mocked method. I have a mocked subscriber which is called by the code under test and i would like to verify the invocation of the notify() method.

class Subscriber:
    def notify(self, event):
        pass

I'm using the following snippet to unpack the arguments and verify two invocations:

calls= self.subscriber.notify.call_args_list
event1 = calls[0][0][0]
event2 = calls[1][0][0]

assert_that(event1, instance_of(CreatedEvent))
assert_that(event1.file.name, equal_to("foo.txt"))

But the two lines to unpack the events are very clumsy and far away from readable code. Does somebody know a better approach to unpack the arguments?

Thanks a lot!

kKdH
  • 512
  • 1
  • 8
  • 17

2 Answers2

1

If you're trying to assert the same thing for events in each call, then using a simple for loop might help:

for call in calls:
    event = call[0][0]
    assert_that(event, instance_of(CreatedEvent))
    assert_that(event.file.name, equal_to("foo.txt"))
Anuj Gautam
  • 1,235
  • 1
  • 7
  • 14
0

You can use assert_has_calls

calls.assert_has_calls(call(ANY, ...), call(...))
Dan
  • 1,874
  • 1
  • 16
  • 21