void PenUp()
{
int a = 0;
};
void PenDown()
{
int b = 0;
};
};
class MockTurtleChild : public TurtleChild {
public:
MOCK_METHOD0(PenUp, void());
MOCK_METHOD0(PenDown, void());
};
class PainterChildRef
{
TurtleChild turtle;
public:
PainterChildRef(TurtleChild& turtle)
: turtle(turtle) {}
bool DrawCircle(int, int, int) {
turtle.PenDown();
return true;
}
};
TEST(PainterTest, ChildRefCanDrawSomething) {
MockTurtleChild turtle;
EXPECT_CALL(turtle, PenDown())
.Times(AtLeast(1));
PainterChildRef painter(turtle);
EXPECT_TRUE(painter.DrawCircle(0, 0, 10));
}
结果4:
结论四:
测试用例失败,以引用类型传入的成员变量本身不具备多态特性,因此gmock不支持
结论
本文通过四个场景,层层递进,深入的剖析了gmock的使用,希望大家在写代码之前早做打算,避免大动干戈,返工重来。但是从另一个方面来说,接口隔离, p-impl 惯用法等技术,应该是一个c++老鸟的必备法宝,可见好多东西都是有其道理的,前期不了解,后期只能花更多的精力取弥补,要么推翻重构,要么直接放弃,无知者无畏,no zuo, no die..