有没有一种方法可以使用&quot&quot_called_with&quot&quot&quot那只是检查特定的单词吗?
我正在尝试使用assert_called_with
来断言传递给模拟对象的参数。该功能只有一个论点,但是该论点很大。 {“ A”:{“ B”:{“ C”:{“ d”:{“ key”:“ keyword”,“任何”:“ aaa”,“ nothing”,“ nothing”:“ bbbb”}}}}}}} }
我只关心“键”:“关键字”
不在乎其他东西,有没有办法这样做?
I am trying to use assert_called_with
to assert the argument that is passed to the mocked object. There is only one argument of that function but the argument is very large.{"A":{"B": {"C" : {"D": {"key": "keyword", "any": "aaa", "anything": "bbbb"}}}}
I only care about "key": "keyword"
exists and do not care about other stuff, is there a way to do so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您的函数具有一个参数,那就是嵌套词典,并且应在某种级别包含特定的键/值对,则必须使用某些函数手动迭代该词典。
您可以从
call_args_list
获得模拟函数的所有调用参数。每个条目都包含一个带有位置参数列表和关键字参数的元组,例如call_args_list [0] [0] [0] [0]
包含第一个呼叫的第一个位置参数。假设您的测试函数总是完全有一个参数,这被称为位置参数,您可以做类似的事情:
一些注释:
args [0]
),args [1]
关键字参数)dict_contains_key_value
可以在需要时进行优化和扩展,这只是一个简单的示例Assuming that your function has one argument, that is a nested dictionary and shall on some level contain the specific key/value pair, you have to iterate over that dictionary manually using some function.
You get the arguments of all calls of your mocked function from
call_args_list
. Each entry contains a tuple with the list of positional arguments and the keyword arguments, e.g.call_args_list[0][0][0]
contains the first positional argument of the first call.Assuming that your tested function have always exactly one arguments, that is called as a positional argument, you can do something like this:
A few notes:
args[0]
)args[1]
would give you the dictionary of keyword arguments)dict_contains_key_value
can be optimized and extended if needed, this is just a simple example