I have two mocks. Only one of them should be called for a single run and I would like to use the expectations to figure out if the execute() function is successful without knowing from the given preconditions.
How can this be achieved?
Mock1 successMock;
Mock2 failMock;
EXPECT_CALL(successMock, performOnSuccess()).Times(1);
EXPECT_CALL(failMock, performOnFail()).Times(0);
execute(successMock, failMock);
Either above should be valid or below expectations but only one of them.
EXPECT_CALL(successMock, performOnSuccess()).Times(0);
EXPECT_CALL(failMock, performOnFail()).Times(1);
What you expect is that one (and only one) of the collaborators has been called.
One viable solution (as presented in other languages) is to make these mocks to increment a shared counter in the test scope.
You can achieve this with GoogleMock by defining actions. It would be something like this:
SuccessMock successMock;
FailMock failMock;
int callCounter = 0;
ON_CALL(successMock, performOnSuccess())
.WillByDefault(InvokeWithoutArgs([&]()
{
callCounter++;
});
ON_CALL(failMock, performOnFailure())
.WillByDefault(InvokeWithoutArgs([&]()
{
callsCounter++;
});
execute(successMock, failMock);
ASSERT_THAT(callsCounter, Eq(1));
Having said this, this test has some randomness in it that I don't really like. You should be end up having one test expecting failure and another one expecting success.
I wrote an simplified gist for this.
With the help of googlemock and googletest, I set up a test that checks that different mocked errors are correctly handled by the method under test. Basically my code looks like this:
// setup mock object, and object under test
// setup initial EXPECT_CALL expectations
// this expected method call in the middle mocks a failure
EXPECT_CALL(*mock, method(arg_in)).
Times(1).
WillOnce(Throw(exception_of_type_A));
// setup cleanup EXPECT_CALL expectations
// Now invoke method in object under test.
// Expect exception has been translated to different type.
EXPECT_THROW(x.method_under_test(), exception_type_B);
// destructor of mock object will check the mock method invocations
Now my mocked method that fails here can not only fail by throwing an exception of type A, but also by throwing an exception of type B, or by returning an unexpected return value.
I can implement this easily enough by copying and pasting the complete TEST() and just changing what the misbehaving mocked method1 will do. But this will make the code messy. Even if I document that these 3 tests are exactly the same except for how the mocked method1 fails in the WillOnce() action specification, a human reader would still have to compare carefully if this is still true.
What would be the correct way in googletest/googlemock to share the common code between the three TESTS and just have them differ in the WillOnce() action?
To my mind comes: Macros, loops over a container with WillOnce() actions, googletest fixtures, static helper methods for setup and cleanup.
I am still new to googletest and not sure how to address this.
For now, I implement the test logic in a templated static function that accepts an action as a parameter:
template <typename A>
static void paramererizeable_test(A failingAction) {
// set everything up
EXPECT_CALL(*mock, method(arg_in)).
Times(1).
WillOnce(failingAction);
// set up more expectations
// trigger the calls
}
TEST(Section, Method) {
paramererizeable_test(Throw(exception_of_type_A));
paramererizeable_test(Throw(exception_of_type_B));
paramererizeable_test(Return(unexpected_return_value));
}
Not sure if this is how it should be done, or if there is a better way, but it works and is readable.
Is it possible to mock out any method calls on a stub and have it return the same response?
I'm looking to do something like this:
$mock->expects($this->any())
->method($this->any())
->willReturn($mock);
I realize that this isn't a best practice, but I already have this section of code covered. All the methods that actually get called should return the object itself too.
I've tried the above example and not adding the call to method. Neither worked and I can't find anything in the documentation.
I don't understand the difference between ON_CALL and EXPECT_CALL when using it to
specify the default action.
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 ...)
There are subtle but significant differences between the two statements. EXPECT_CALL sets expectation on a mock calls. Writing
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,
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_CALLs without specifying the action explicitly. E.g.,
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_CALLs, you don't have to mock's specify the behavior every time:
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
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
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.
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 0x01
EXPECT_CALL(mock, methodX(_)).WillRepeatedly(Return(0x01)); means that it is expected that the method will be called, and every call will return 0x01
By the way, there is a Setting default actions in their cheat sheet, which says :
To customize the default action for a particular method, use ON_CALL():
ON_CALL(mock_object, method(matchers))
.With(multi_argument_matcher) ?
.WillByDefault(action);
Here is the "official" explanation about the most important differences between ON_CALL and EXPECT_CALL as explained in the gMock Cookbook.
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 must be called with the given arguments, for the given number of times (and in the given order when you specify the order too).
Since EXPECT_CALL does more, isn't it better than ON_CALL?
Not really. Every EXPECT_CALL adds a constraint on the behavior of the code under test. Having more constraints than necessary is bad - even worse than not having enough constraints.
This may be counter-intuitive. How could tests that verify more be worse than tests that verify less? Isn't verification the whole point of tests?
The answer, lies in what a test should verify. A good test verifies the contract of the code. If a test over-specifies, it doesn't leave enough freedom to the implementation. As a result, changing the implementation without breaking the contract (e.g. refactoring and optimization), which should be perfectly fine to do, can break such tests. Then you have to spend time fixing them, only to see them broken again the next time the implementation is changed.
Keep in mind that one doesn't have to verify more than one property in one test. In fact, it's a good style to verify only one thing in one test. If you do that, a bug will likely break only one or two tests instead of dozens (which case would you rather debug?). If you are also in the habit of giving tests descriptive names that tell what they verify, you can often easily guess what's wrong just from the test log itself.
So use ON_CALL by default, and only use EXPECT_CALL when you actually intend to verify that the call is made.
See here https://github.com/google/googletest/blob/master/docs/gmock_cook_book.md#knowing-when-to-expect-useoncall
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).
One difference is that the ON_CALL behavior (default behavior) and the EXPECT_CALL expectations are cleared differently.
https://github.com/google/googletest/blob/master/googlemock/docs/cheat_sheet.md#verifying-and-resetting-a-mock
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.
I'm a beginner in Mockito.
The SUT, Foo, has one collaborator, Bar, and in one particular test case, Foo should not call any of the methods of Bar.
How do I go about verifying it?
Try this:
verifyZeroInteractions(bar);
As explained here.