Reputation: 343
Has anyone ever seen seen odd behavior in gmock when following an ON_CALL statement with an EXPECT_CALL statement? For me, the EXPECT_CALL statement in the following code doesn't work (it doesn't actually enforce the Times part):
ON_CALL(myMockObject, myMockMethod()).WillByDefault(Return("hello mock")));
EXPECT_CALL(myMockObject, myMockMethod()).Times(99999);
myMockObject.myMockMethod();
Other solutions that I've tried:
Overriding the myMockMethod from the super class and have it simply return a string literal. The problem with this is that I can't determine how many times it's been called later on.
Skipping the ON_CALL part in favor of something like this:
EXPECT_CALL(myMockObject, myMockMethod())
.Times(1)
.WillRepeatedly(Return("hello mock"));
This results in a compilation error.
Also of note, the string literal I'm using in this example is custom in reality and something that gmock won't be able to come up with a default for (such as bool).
Upvotes: 2
Views: 1784
Reputation: 78330
You have some other error in your original code which is not being mentioned in your question. The code provided in the question behaves as you expected if you construct a minimal self-contained example.
For example, the following code:
#include <string>
#include "gmock/gmock.h"
using ::testing::Return;
struct MyClass {
virtual ~MyClass() {}
virtual std::string myMockMethod() = 0;
};
struct MyMockClass : MyClass {
MOCK_METHOD0(myMockMethod, std::string());
};
TEST(MyClass, Fails) {
MyMockClass myMockObject;
ON_CALL(myMockObject, myMockMethod()).WillByDefault(Return("hello mock"));
EXPECT_CALL(myMockObject, myMockMethod()).Times(99999);
myMockObject.myMockMethod();
}
TEST(MyClass, Passes) {
MyMockClass myMockObject;
EXPECT_CALL(myMockObject, myMockMethod()).Times(1).WillRepeatedly(Return("hello mock"));
myMockObject.myMockMethod();
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
produces the following (expected) output:
[==========] Running 2 tests from 1 test case. [----------] Global test environment set-up. [==========] 2 tests from MyClass [ RUN ] MyClass.Fails ..\src\main.cc(18): error: Actual function call count doesn't match EXPECT_CALL(myMockObject, myMockMethod())... Expected: to be called 99999 times Actual: called once - unsatisfied and active [ FAILED ] MyClass.Fails (0 ms) [ RUN ] MyClass.Passes [ OK ] MyClass.Passes (0 ms) [----------] 2 tests from MyClass (2 ms total) [----------] Global test environment tear-down [==========] 2 tests from 1 test case ran. (2 ms total) [ PASSED ] 1 test. [ FAILED ] 1 test, listed below: [ FAILED ] MyClass.Fails 1 FAILED TEST
If you want your mock object to be held in a test fixture, you can do:
class MyClassTest : public testing::Test {
protected:
MyMockClass myMockObject_;
};
TEST_F(MyClassTest, Fails) {
ON_CALL(myMockObject_, myMockMethod()).WillByDefault(Return("hello mock"));
EXPECT_CALL(myMockObject_, myMockMethod()).Times(99999);
myMockObject_.myMockMethod();
}
Upvotes: 2
Reputation: 343
Mock::VerifyAndClearExpectations(&myMockObject);
This did the trick. I'm still not sure how expectations are managed behind the scenes, but this got me working.
Any further explanation of this would be greatly appreciated, though.
Upvotes: 0