我寫了一個嘲笑類的小測試。當我運行它的時候,首先我得到了一個警告:一個無趣的模擬函數被調用,然後測試失敗,因爲沒有滿足期望,那就是至少調用一次模擬函數。有趣的是,當我看到上面的警告消息時,調用該函數。無趣的模擬函數調用bla()&&期望:至少被調用一次bla()?
您對這件事有什麼想法嗎?
謝謝!
編輯:這是我的代碼結構:
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
沒有人,直到你證明你的代碼有什麼想法! –
你說得對,我剛剛用我的代碼示例進行了編輯。謝謝! – emmerich