① 修改mockcpp的接口文件(追加一个宏),在mokc.h中追加MOCKER_CPP宏:
#define MOCKER_CPP(api, TT) MOCKCPP_NS::mockAPI(#api, reinterpret_cast<TT>(api))
② 使用例子:
#ifndef SAMPLE_H
#define SAMPLE_H
#include <gtest/gtest.h>
#include <mockcpp/mockcpp.hpp>
#include <memory>
using namespace std;
class ICalc
{
public:
virtual ~ICalc() {};
virtual int add(int a, int b) = 0;
virtual int add4(int a, int b) = 0;
};
class CalcImpl
: public ICalc
{
public:
virtual int add(int a, int b) {
return a + b;
}
virtual int add4(int a, int b) {
return a + b;
}
static int add2(int a, int b) {
return a + b;
}
int add3(int a, int b) {
return a + b;
}
};
class CalcImpl2
: public ICalc
{
public:
virtual int add(int a, int b) {
return a + b;
}
virtual int add4(int a, int b) {
return a + b;
}
static int add2(int a, int b) {
return a + b;
}
int add3(int a, int b) {
return a + b;
}
int add3(int a, int b, int c) {
return a + b + c;
}
};
class TestUse
{
public:
int add(int a, int b) {
calc.add3(a, b);
}
char* getData() {
char* sd = (char *)malloc(sizeof(char) * 100);
return sd;
}
CalcImpl calc;
CalcImpl calc2;
};
class CalcTest : public testing::Test
{
protected:
CalcTest() = default;
~CalcTest() = default;
virtual void SetUp() override
{
}
virtual void TearDown() override
{
}
};
// 测试纯虚类中的纯虚函数
TEST_F(CalcTest, test_xxx_001)
{
MockObject<ICalc> calc;
MOCK_METHOD(calc, add)
.stubs()
.will(returnValue(11));
EXPECT_EQ(11, calc->add(1, 1));
GlobalMockObject::verify();
}
// 测试纯虚类中的纯虚函数
TEST_F(CalcTest, test_xxx_002)
{
MockObject<CalcImpl> calc;
MOCK_METHOD(calc, add)
.stubs()
.will(returnValue(11));
EXPECT_EQ(11, calc->add(1, 1));
GlobalMockObject::verify();
}
// 测试类中的static函数
TEST_F(CalcTest, test_xxx_003)
{
MOCKER(&CalcImpl::add2)
.stubs()
.will(returnValue(11));
EXPECT_EQ(11, CalcImpl::add2(1, 1));
GlobalMockObject::verify();
}
// 测试已存在对象中的虚函数函数mock
TEST_F(CalcTest, test_xxx_004)
{
// add4和add为虚函数,第一个为this指针
CalcImpl impl;
MOCKER_CPP(&CalcImpl::add4, int (*)(CalcImpl*, int, int))
.stubs()
.will(returnValue(11));
MOCKER_CPP(&CalcImpl::add, int (*)(CalcImpl*, int, int))
.expects(once())
.with(any(), eq(1), eq(1))
.will(returnValue(222));
EXPECT_EQ(11, impl.add4(1, 1));
EXPECT_EQ(222, impl.add(1, 1));
GlobalMockObject::verify();
}
// 测试已存在对象中的成员函数mock
TEST_F(CalcTest, test_xxx_005)
{
CalcImpl impl;
MOCKER_CPP(&CalcImpl::add3, int (*)(CalcImpl*, int, int))
.expects(once())
.with(any(), eq(1), eq(1))
.will(returnValue(11));
EXPECT_EQ(11, impl.add3(1, 1));
GlobalMockObject::verify();
}
// 测试嵌套调用对象中的成员函数mock(开启mock)
TEST_F(CalcTest, test_xxx_006)
{
TestUse tu;
MOCKER_CPP(&CalcImpl::add3, int (*)(CalcImpl*, int, int))
.expects(once())
.with(eq(&tu.calc), eq(1), eq(1))
.will(returnValue(11));
EXPECT_EQ(11, tu.add(1, 1));
GlobalMockObject::verify();
}
// 测试嵌套调用对象中的成员函数mock(关闭mock)
TEST_F(CalcTest, test_xxx_007)
{
TestUse tu;
EXPECT_EQ(2, tu.add(1, 1));
GlobalMockObject::verify();
}
// 测试malloc的hook
//TEST_F(CalcTest, test_xxx_008)
//{
// TestUse tu;
// MOCKER(malloc)
// .expects(once())
// .will(returnValue(NULL));
// GlobalMockObject::verify();
//}
// 测试嵌套调用对象中的成员函数mock(关闭mock)
TEST_F(CalcTest, test_xxx_009)
{
TestUse tu;
EXPECT_EQ(2, tu.add(1, 1));
GlobalMockObject::verify();
}
// 测试只针对某个对象自己的虚函数mock
TEST_F(CalcTest, test_xxx_010)
{
CalcImpl tu;
CalcImpl2 tu2;
int (CalcImpl::*pfn)(int, int) = &CalcImpl::add4;
int (CalcImpl2::*pfn2)(int, int) = &CalcImpl2::add4;
MOCKER_CPP(tu.*pfn, int (*)(CalcImpl*, int, int))
.expects(once())
.with(any(), eq(1), eq(1))
.will(returnValue(11));
printf("1: %p\n", (void*)(tu.*pfn));
printf("2: %p\n", (void*)(tu2.*pfn2));
EXPECT_EQ(11, tu.add4(1, 1));
EXPECT_EQ(2, tu2.add4(1, 1));
GlobalMockObject::verify();
}
// 测试重载的情况
TEST_F(CalcTest, test_xxx_011)
{
CalcImpl2 tu2;
int (CalcImpl2::*pfn)(int, int, int) = &CalcImpl2::add3;
int (CalcImpl2::*pfn2)(int, int) = &CalcImpl2::add3;
MOCKER_CPP(tu2.*pfn, int (*)(CalcImpl2*, int, int, int))
.expects(once())
.with(any(), eq(1), eq(1), eq(1))
.will(returnValue(222));
MOCKER_CPP(tu2.*pfn2, int (*)(CalcImpl2*, int, int))
.expects(once())
.with(any(), eq(1), eq(1))
.will(returnValue(111));
EXPECT_EQ(222, tu2.add3(1, 1, 1));
EXPECT_EQ(111, tu2.add3(1, 1));
GlobalMockObject::verify();
}
#endif