我想模拟一个具有指针参数
的函数class I_Shaft
{
public:
virtual ~I_Shaft() {
}
virtual void GetFloor(I_Floor* FloorPointer) = 0;
};
模拟界面:
class Mock_I_Shaft : public I_Shaft
{
public:
MOCK_METHOD1(GetFloor, void(I_Floor* FloorPointer));
};
模拟接口Mock_I_Floor需要默认和复制构造函数(否则它不会被编译):
class Mock_I_Floor : public I_Floor
{
public:
Mock_I_Floor() {}
Mock_I_Floor(const Mock_I_Floor& f) {}
virtual ~Mock_I_Floor() {};
MOCK_METHOD0(GetPosition, uint16_t());
};
要测试的实现:
C_HZS::C_HZS(I_Shaft& refShaft) : //constructor initializing refShaft
refShaft(refShaft)
{
}
void C_HZS::run(void)
{
I_Floor* ptrFloor = NULL;
refShaft.GetFloor(ptrFloor);
if(ptrFloor == NULL){
std::cerr << "there is no valid pointer" <<std::endl;
return;
}
floorPosition = ptrFloor->GetPosition();
std::cout << "floorPosition: " << floorPosition << std::endl;
}
这是我的测试:
TEST(HZS, testFloorPosition) {
Mock_I_Shaft shaft;
Mock_I_Floor floor;
Mock_I_Floor* pFloorMocked= &floor;
C_HZS dutObj(shaft);
EXPECT_CALL(shaft, GetFloor(_))
.Times(AtLeast(1))
.WillOnce(SetArgPointee<0>(*pFloorMocked)
);
dutObj.run();
EXPECT_FALSE(...);
}
问题是:我无法将pFloorMocked注入模拟函数。在run()函数中,ptrFloor始终保持NULL。这是什么原因?有没有其他方法来模拟这个功能?