我用一个模拟类写了一个小测试。当我运行它时,首先我得到一个警告,即调用了一个无趣的模拟函数,然后测试失败,因为没有满足期望,即模拟函数至少被调用了一次。有趣的是,当我看到上面的警告消息时,调用了该函数。
你对这个问题有什么想法吗?
谢谢!
编辑:这是我的代码结构:
class Bla {
public:
Bla();
virtual ~Bla();
virtual float myFunction();
}
class MockBla : public Bla {
MockBla();
~MockBla();
MOCKMETHOD0(myFunction, float());
}
class CallerClass {
public:
CallerClass() { MockBla* myMock = new MockBla(); }
virtual ~CallerClass();
myCallingFunction() { myMock->myFunction(); }
}
class MyTestClass : public ::testing::Test {
//....
TEST(myTest, testMyFunction) {
MockBla mockBla;
EXPECT_CALL(mockBla, myFunction()).Times(AtLeast(1));
CallerClass* callerClass;
callerClass = new CallerClass();
callerClass->myCallingFunction();
}
//....
}
结果:
[ RUN ] MyTestClass.testMyFunction
GMOCK WARNING:
Uninteresting mock function call - returning default value.
Function call: myFunction()
Returns: 0
Stack trace:
MyTestClass.cpp:99: Failure
Actual function call count doesn't match EXPECT_CALL(mockBla, myFunction())...
Expected: to be called at least once
Actual: never called - unsatisfied and active