我已经写了一个简单的示例代码来了解用于单元测试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