Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:从unittest.mock解包call_args_列表_Python_Python 3.x_Mocking_Python Unittest_Python Unittest.mock - Fatal编程技术网

Python:从unittest.mock解包call_args_列表

Python:从unittest.mock解包call_args_列表,python,python-3.x,mocking,python-unittest,python-unittest.mock,Python,Python 3.x,Mocking,Python Unittest,Python Unittest.mock,我想解压一个模拟方法的参数。我有一个被测试代码调用的模拟订户,我想验证notify()方法的调用 我使用以下代码片段来解压参数并验证两个调用: 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_t

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

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

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"))
但解压事件的两行代码非常笨拙,而且远离可读代码。 有人知道一个更好的方法来解开这些论点吗


非常感谢

如果您试图为每个调用中的事件断言相同的内容,那么使用简单的for循环可能会有所帮助:

for call in calls:
    event = call[0][0]
    assert_that(event, instance_of(CreatedEvent))
    assert_that(event.file.name, equal_to("foo.txt"))

您可以使用
assert\u has\u调用

calls.assert_has_calls(call(ANY, ...), call(...))

您将如何处理
event1
event2
?我不明白…更新了问题::-)
calls.assert_has_calls(call(ANY, ...), call(...))