我已經寫了一個簡單的示例代碼來了解用於單元測試GMOCK:測試用例失敗國家,但返回的值是true
#include <iostream>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using ::testing::AtLeast;
class A {
public:
void ShowPub1()
{
std::cout << "A::PUBLIC::SHOW..1" << std::endl;
}
int ShowPub2(int x)
{
std::cout << "A::PUBLIC::SHOW..2" << std::endl;
return true;
}
};
class MockA : public A{
public:
MOCK_METHOD0(ShowPub1, void());
MOCK_METHOD1(ShowPub2, int(int x));
};
下面是我的測試代碼 - 我只是想叫類的ShowPub2方法答:我期待的聲明A :: PUBLIC :: SHOW..2打印機會在控制檯 - 但它只是沒有發生,測試用例也未能雖然該方法是硬編碼返回true:
TEST(FirstA, TestCall) {
MockA a;
EXPECT_CALL(a, ShowPub2(2))
.Times(AtLeast(1));
a.ShowPub2(2);
EXPECT_TRUE(a.ShowPub2(2));
}
GMOCK測試代碼執行輸出 - 我不是知道爲什麼輸出A :: PUBLIC :: SHOW..2沒有控制檯和測試用例渲染失敗:
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from FirstA
[ RUN ] FirstA.TestCall
c:\users\user1\documents\c and c++\gmock\gmock1\gmock1\gmock1.cpp(78): error:
Value of: a.ShowPub2(2)
Actual: false
Expected: true
[ FAILED ] FirstA.TestCall (0 ms)
[----------] 1 test from FirstA (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] FirstA.TestCall
1 FAILED TEST
Press any key to continue . . .
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
return 0;
}
嘲笑方法必須是虛擬:https://github.com/google/googlemock/blob/master/googlemock/docs/ForDummies.md。 – Gluttton
我讓我的兩種方法都是虛擬的,但仍是同樣的問題 - 我錯過了什麼?虛擬無效ShowPub1(){}和虛擬INT ShowPub2(INT X){} – Prakash