1. 首先给出第一个运用gtest的demo
#include "stdafx.h"
#include "gmock/gmock-actions.h"
using namespace testing;
class Calculate
{
public:
Calculate(){}
long add(long a,long b){return a+b;}
};
class CalculateMock:public Calculate
{
public:
CalculateMock():Calculate(){}
MOCK_METHOD2(add,long(long a,long b));
};
long testFun(Calculate& cal)
{
return cal.add(2,3);
}
TEST(testMock,testAdd)
{
CalculateMock obj;
long len = 10;
ON_CALL(obj,add(2,3)).WillByDefault(Return(len));
EXPECT_CALL(obj,add(2,3)).Times(1);
//obj.add(2,3);
EXPECT_EQ(10, obj.add(2,3));
}
int _tmain(int argc, _TCHAR* argv[])
{
testing::InitGoogleMock(&argc, argv);
RUN_ALL_TESTS();
return 0;
}
做第一个demo需要注意的事项:
1. 将用到的gtest,gmock,和你自己运用的project用同样的code generation 的形式一致,将 project property->C++->Code Generation: 设置为:Multi-threaded Debug(/MTd)
2. 添加 using namespace testing, 否则会出现‘Return’Identifier not found.这样的错误
So excited to make it work after so much confusion, anxiety.
Fighting!!