0

我想解开模拟方法的参数。我有一个被测试代码调用的模拟订阅者,我想验证 notify() 方法的调用。

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

我正在使用以下代码段来解压缩参数并验证两个调用:

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"))

但是解包事件的两行代码非常笨拙,并且与可读代码相差甚远。有人知道解开论点的更好方法吗?

非常感谢!

4

2 回答 2

1

如果您尝试在每次调用中为事件声明相同的内容,那么使用简单的 for 循环可能会有所帮助:

for call in calls:
    event = call[0][0]
    assert_that(event, instance_of(CreatedEvent))
    assert_that(event.file.name, equal_to("foo.txt"))
于 2018-01-18T08:23:21.450 回答
0

您可以使用assert_has_calls

calls.assert_has_calls(call(ANY, ...), call(...))
于 2018-01-23T17:06:05.683 回答