C++ gmock 设置默认操作 / ON_CALL 与 EXPECT_CALL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13933475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
gmock setting default actions / ON_CALL vs. EXPECT_CALL
提问by Nicoretti
I don't understand the difference between ON_CALL and EXPECT_CALL when using it to specify the default action.
我不明白使用它来指定默认操作时 ON_CALL 和 EXPECT_CALL 之间的区别。
So far I noticed/learned there are two ways to adjust the default action of a mock:
到目前为止,我注意到/了解到有两种方法可以调整模拟的默认操作:
ON_CALL(mock, methodX(_)).WillByDefault(Return(0x01));
or
或者
EXPECT_CALL(mock, methodX(_)).WillRepeatedly(Return(0x01));
Could someone explain to me:
有人可以向我解释一下:
- The difference between the two methods
- The ups and downs of each one
- When is it appropriate to use them (what kind of setup ...)
- 两种方法的区别
- 每个人的起起落落
- 什么时候适合使用它们(什么样的设置......)
回答by VladLosev
There are subtle but significant differences between the two statements. EXPECT_CALL
sets expectation on a mock calls. Writing
两种说法之间存在细微但显着的差异。EXPECT_CALL
对模拟调用设置期望。写作
EXPECT_CALL(mock, methodX(_)).WillRepeatedly(do_action);
tells gMock that methodX
may be called on mock
any number of times with any arguments, and when it is, mock
will perform do_action
. On the other hand,
告诉 gMockmethodX
可以mock
使用任何参数调用任意次数,并且当它调用时,mock
将执行do_action
. 另一方面,
ON_CALL(mock, methodX(_)).WillByDefault(do_action);
tells gMock that whenever methodX
is invoked on mock
, it should perform do_action
. That feature is helpful in a scenario where you have to write many expectations on your mock, and most/all of them have to specify the same action -- especially if it's complex. You can specify that action in ON_CALL
, and then write EXPECT_CALL
s without specifying the action explicitly. E.g.,
告诉 gMock 无论何时methodX
被调用mock
,它都应该执行do_action
。该功能在您必须在模拟上编写许多期望并且大多数/所有期望都必须指定相同操作的场景中很有用 - 特别是如果它很复杂。您可以在 中指定该操作ON_CALL
,然后EXPECT_CALL
在不显式指定该操作的情况下写入s。例如,
ON_CALL(mock, Sign(Eq(0), _))
.WillByDefault(DoAll(SetArgPointee<1>("argument is zero"), Return(0)));
ON_CALL(mock, Sign(Gt(0), _))
.WillByDefault(DoAll(SetArgPointee<1>("argument is positive"), Return(1)));
ON_CALL(mock, Sign(Lt(0), _))
.WillByDefault(DoAll(SetArgPointee<1>("argument is negative"), Return(-1)));
Now, if you have to write a lot of EXPECT_CALL
s, you don't have to mock
's specify the behavior every time:
现在,如果您必须编写很多EXPECT_CALL
s,则不必mock
每次都指定 s 的行为:
EXPECT_CALL(mock, Sign(-4, _));
EXPECT_CALL(mock, Sign(0, _));
EXPECT_CALL(mock, Sign(1, _)).Times(2);
EXPECT_CALL(mock, Sign(2, _));
EXPECT_CALL(mock, Sign(3, _));
EXPECT_CALL(mock, Sign(5, _));
In another example, assuming Sign returns int
, if you write
在另一个例子中,假设 Sign 返回int
,如果你写
ON_CALL(mock, Sign(Gt(0), _)).WillByDefault(Return(1));
EXPECT_CALL(mock, Sign(10, _));
the call mock.Sign(10)
will return 1 as ON_CALL
provides default behavior for a call specified by EXPECT_CALL
. But if you write
该调用mock.Sign(10)
将返回 1,因为ON_CALL
为由 指定的调用提供默认行为EXPECT_CALL
。但是如果你写
EXPECT_CALL(mock, Sign(Gt(0), _).WillRepeatedly(Return(1));
EXPECT_CALL(mock, Sign(10, _));
the invocation of mock.Sign(10, p)
will return 0. It will be matched against the second expectation. That expectation specifies no explicit action and gMock will generate a default action for it. That default action is to return a default value of the return type, which is 0 for int
. The first expectation will be totally ignored in this case.
的调用mock.Sign(10, p)
将返回 0。它将与第二个期望匹配。该期望没有指定明确的动作,gMock 将为它生成一个默认动作。该默认操作是返回返回类型的默认值,对于int
. 在这种情况下,第一个期望将被完全忽略。
回答by B?ови?
ON_CALL(mock, methodX(_)).WillByDefault(Return(0x01));
EXPECT_CALL(mock, methodX(_)).WillRepeatedly(Return(0x01));
As you said, these two lines are doing exactly the same thing, therefore there are no differences at all. Use either way to set a default action as you please.
就像你说的,这两条线做的完全一样,所以根本没有区别。使用任何一种方式都可以根据需要设置默认操作。
However, there is a logical difference :
但是,存在逻辑差异:
ON_CALL(mock, methodX(_)).WillByDefault(Return(0x01));
means that the method might be called, and if that happens, every call will return 0x01EXPECT_CALL(mock, methodX(_)).WillRepeatedly(Return(0x01));
means that it is expected that the method will be called, and every call will return 0x01
ON_CALL(mock, methodX(_)).WillByDefault(Return(0x01));
意味着该方法可能会被调用,如果发生这种情况,每次调用都会返回 0x01EXPECT_CALL(mock, methodX(_)).WillRepeatedly(Return(0x01));
表示预计会调用该方法,每次调用都会返回0x01
By the way, there is a Setting default actionsin their cheat sheet, which says :
顺便说一句,他们的备忘单中有一个设置默认操作,上面写着:
To customize the default action for a particular method, use ON_CALL():
要为特定方法自定义默认操作,请使用 ON_CALL():
ON_CALL(mock_object, method(matchers))
.With(multi_argument_matcher) ?
.WillByDefault(action);
回答by kula85
There are basically two constructs for defining the behavior of a mock object: ON_CALL and EXPECT_CALL. The difference? ON_CALL defines what happens when a mock method is called, but doesn't imply any expectation on the method being called. EXPECT_CALL not only defines the behavior, but also sets an expectation that the method will be called with the given arguments, for the given number of times (and in the given order when you specify the order too).
基本上有两种用于定义模拟对象行为的构造:ON_CALL 和 EXPECT_CALL。区别?ON_CALL 定义了调用模拟方法时会发生什么,但并不暗示对被调用方法的任何期望。EXPECT_CALL 不仅定义了行为,而且还设置了一个期望,即该方法将使用给定的参数调用给定的次数(并且在您指定顺序时也按照给定的顺序)。
回答by Martin G
One difference is that the ON_CALL
behavior (default behavior) and the EXPECT_CALL
expectations are cleared differently.
一个区别是ON_CALL
行为(默认行为)和EXPECT_CALL
期望的清除方式不同。
using ::testing::Mock;
...
// Verifies and removes the expectations on mock_obj;
// returns true if and only if successful.
Mock::VerifyAndClearExpectations(&mock_obj);
...
// Verifies and removes the expectations on mock_obj;
// also removes the default actions set by ON_CALL();
// returns true if and only if successful.
Mock::VerifyAndClear(&mock_obj);
This can be used to clear expectations at some point in your test, but still keep the default behavior of the mock object. Notice that this is not the case for StrictMock
objects since they will not allow the test to pass without actual expectations, even with a defined default behavior setup with ON_CALL
.
这可用于在测试中的某个时刻清除预期,但仍保留模拟对象的默认行为。请注意,StrictMock
对象的情况并非如此,因为它们不允许测试在没有实际预期的情况下通过,即使使用ON_CALL
.