如何使用通配符检查模拟调用? [英] How to check for mock calls with wildcards?
问题描述
我正在编写单元测试,并想检查具有函数对象的调用,如下所示:
I am writing unit tests, and want to check for a call which has a function object, like so:
call(u'mock', u'foobar',
如何在不重现 lambda 的情况下检查 call() 是否具有我想要的所有参数?
How do I check that the call() has all the parameters I want, without reproducing the lambda?
我想澄清一下我正在使用这里的 mock
库:http://mock.readthedocs.org/en/latest/.我上面展示的 call
是对 MagicMock
对象的调用,我想使用 assert_has_calls
检查它.
I wanted to clarify that I am using the mock
library from here: http://mock.readthedocs.org/en/latest/. The call
I showed above is a call on a MagicMock
object, which I want to check using assert_has_calls
.
推荐答案
我终于知道如何做我想做的事了.基本上,当使用 assert_has_calls
时,我希望一个参数匹配而不管它是什么(因为我无法在测试期间每次都重新创建 lambda
).
I finally found out how to do what I want. Basically, when using assert_has_calls
, I wanted one parameter to match regardless of what it was (because I can't recreate the lambda
every time during test).
这样做的方法是使用 mock.ANY
.
The way to do it is to use mock.ANY
.
因此,在我的示例中,这可以匹配调用:
So, in my example, this could match the call:
mocked_object.assert_has_calls([
call('mock', 'foobar', mock.ANY, 10)
])
这篇关于如何使用通配符检查模拟调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!