待测函数
class Foo {
public
virtual void get1(int x, int y) const = 0;
virtual int get2() = 0;
}
mock类:
MOCK_METHODx(函数名,返回类型+形参列表)
x表示形参个数
include MockFoo : public Foo {
public:
MOCK_METHOD2(get1, int(int x, int y));
MOCK_METHOD(get2, int());
}
测试中使用mock
MockFoo mf;
EXPECT_CALL(mf, get1(1, _))
.WillRepeatedly(testing::Return(200)); //_代表通配符
EXPECT_CALL(mf, get2)
.WillRepeatedly(testing::Return(200));
使用lambda表达式进行复杂的mock
EXPECT_CALL(mf, get1)
.WillRepeatedly(Invoke(
[=](int x, int y) -> int {return x * y}
));