简体   繁体   English

google mock - 怎么说“必须使用某个参数调用一次函数,但可以使用不同的参数调用多次”?

[英]google mock - how to say “function must be called ONCE with a certain parameter but ok to be called many times with different parameters”?

I need to detect that a given function has been called exactly ONCE with a certain set of arguments.我需要检测给定的函数是否已使用一组特定的参数被精确调用一次。

EXPECT_CALL(Mock_Obj, func("abc")).Times(1)

but it's ok for that function to be called with different arguments any number of times.但是可以使用不同的参数多次调用该函数。

How do I express that?我该如何表达?

In Google Mock, later expectations override earlier ones (more details in the docs ), so you can write this:在 Google Mock 中,后来的期望会覆盖早期的期望( 文档中的更多详细信息),因此您可以这样写:

EXPECT_CALL(Mock_Obj, func(_)).Times(AnyNumber());
EXPECT_CALL(Mock_Obj, func("abc")).Times(1);

Here is a little more info.这里有更多信息。 Do like VladLosev said .就像 VladLosev 所说的那样 Put the default (most generic because it allows any argument with the _ character) case first and the more specific cases after , to override the default case if they match first.将默认情况(最通用,因为它允许任何带有_字符的参数)放在最前面,更具体的情况放在之后,如果它们首先匹配,则覆盖默认情况。 I say that the latter EXPECT_CALL() s may match first because gmock does a reverse search for matches, meaning it tries to match the last EXPECT_CALL() first , then it moves its way upwards, stopping and exiting this process at the first match it finds for the call, in this reverse order.我说,后者EXPECT_CALL() S可以匹配,因为gmock做火柴反向搜索,这意味着它尝试匹配的最后EXPECT_CALL()第一,然后移动它的方式向上,停止,并在第一场比赛就退出这个过程以相反的顺序查找调用。

EXPECT_CALL(Mock_Obj, func(_)).Times(AnyNumber());
EXPECT_CALL(Mock_Obj, func("abc")).Times(1);
EXPECT_CALL(Mock_Obj, func("def")).Times(1);
EXPECT_CALL(Mock_Obj, func("cab")).Times(1);
EXPECT_CALL(Mock_Obj, func("bac")).Times(1);

From the "gMock for Dummies {#GMockForDummies}" guide :来自“傻瓜的 gMock {#GMockForDummies}”指南

Using Multiple Expectations {#MultiExpectations}使用多个期望 {#MultiExpectations}

So far we've only shown examples where you have a single expectation.到目前为止,我们只展示了您有单一期望的示例。 More realistically, you'll specify expectations on multiple mock methods which may be from multiple mock objects.更现实的是,您将指定对可能来自多个模拟对象的多个模拟方法的期望。

By default, when a mock method is invoked, gMock will search the expectations in the reverse order they are defined, and stop when an active expectation that matches the arguments is found (you can think of it as "newer rules override older ones.").默认情况下,当调用一个模拟方法时,gMock 将按照它们定义的相反顺序搜索期望,并在找到与参数匹配的活动期望时停止(您可以将其视为“新规则覆盖旧规则”。 )。 If the matching expectation cannot take any more calls, you will get an upper-bound-violated failure.如果匹配的期望不能接受更多的调用,你将得到一个违反上限的失败。 Here's an example:下面是一个例子:

 using ::testing::_; ... EXPECT_CALL(turtle, Forward(_)); // #1 EXPECT_CALL(turtle, Forward(10)) // #2 .Times(2);

If Forward(10) is called three times in a row, the third time it will be an error, as the last matching expectation (#2) has been saturated.如果连续调用Forward(10)三次,第三次就会报错,因为最后一个匹配的期望(#2)已经饱和。 If, however, the third Forward(10) call is replaced by Forward(20) , then it would be OK, as now #1 will be the matching expectation.但是,如果第三个Forward(10)调用被Forward(20)替换,那么就可以了,因为现在 #1 将是匹配的期望。

Note: Why does gMock search for a match in the reverse order of the expectations?注意:为什么 gMock 以与期望相反的顺序搜索匹配项? The reason is that this allows a user to set up the default expectations in a mock object's constructor or the test fixture's set-up phase and then customize the mock by writing more specific expectations in the test body.原因是这允许用户在模拟对象的构造函数或测试装置的设置阶段设置默认期望,然后通过在测试主体中编写更具体的期望来自定义模拟。 So, if you have two expectations on the same method, you want to put the one with more specific matchers after the other, or the more specific rule would be shadowed by the more general one that comes after it.因此,如果您对同一种方法有两种期望,您希望将具有更具体匹配器的一个放在另一个之后,否则更具体的规则会被后面的更通用的规则所掩盖。

Tip: It is very common to start with a catch-all expectation for a method and Times(AnyNumber()) (omitting arguments, or with _ for all arguments, if overloaded).提示:通常以对方法和Times(AnyNumber())的全面期望开始(省略参数,或者如果重载,所有参数都使用_ )。 This makes any calls to the method expected.这使得对方法的任何调用都符合预期。 This is not necessary for methods that are not mentioned at all (these are "uninteresting"), but is useful for methods that have some expectations, but for which other calls are ok.对于根本没有提到的方法(这些是“无趣的”),这不是必需的,但对于有一些期望但其他调用没问题的方法很有用。 See Understanding Uninteresting vs Unexpected Calls.请参阅了解无趣与意外呼叫。

Related:有关的:

  1. google mock - can I call EXPECT_CALL multiple times on same mock object? 谷歌模拟 - 我可以在同一个模拟对象上多次调用 EXPECT_CALL 吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM