Google Mock:对具有不同参数的同一功能有多种期望 [英] Google Mock: multiple expectations on same function with different parameters
问题描述
考虑以下情况:预期某个模拟函数将被多次调用,每次在某个参数中具有不同的值.我想验证该函数确实在某个值列表(例如1,2,5)中的每个值确实被调用过一次.
Consider the case where a certain mocked function is expected to be called several times, each time with a different value in a certain parameter. I would like to validate that the function was indeed called once and only once per value in a certain list of values (e.g. 1,2,5).
另一方面,我想避免定义一个顺序,因为该顺序决定了一定的顺序,这是我想保留的实现细节.
On the other hand, I would like to refrain from defining a sequence as that would dictate a certain order, which is an implementation detail I would like to keep free.
在这种情况下是否有匹配器或其他解决方案?
Is there some kind of matcher, or other solution for this case?
我不确定这是否会以任何方式影响解决方案,但我确实打算将WillOnce(Return(x))与上面列表中的每个值使用不同的x.
I'm not sure if this influences the solution in any way but I do intend to use WillOnce(Return(x)) with a different x per value in the list above.
推荐答案
默认情况下,可以按任何顺序满足gMock期望(正是出于您提到的原因-因此您不必过度指定测试).
By default gMock expectations can be satisfied in any order (precisely for the reason you mention -- so you don't over specify your tests).
在您的情况下,您只需要以下内容:
In your case, you just want something like:
EXPECT_CALL(foo, DoThis(1));
EXPECT_CALL(foo, DoThis(2));
EXPECT_CALL(foo, DoThis(5));
类似的东西:
foo.DoThis(5);
foo.DoThis(1);
foo.DoThis(2);
将满足那些期望.
(此外:如果您没有要限制订单,则应使用InSequence
:
(Aside: If you did want to constrain the order, you should use InSequence
: https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#expecting-ordered-calls-orderedcalls)
这篇关于Google Mock:对具有不同参数的同一功能有多种期望的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!