googlemock:无法创建模拟 class 的对象
googlemock: object of mocked class cannot be created
我有一辆使用 speedInterface 的 class 汽车(上面有一个参考)。
现在我想用 Mock_SpeedInterface.
模拟 speedInterface
class speedInterface
{
public:
virtual ~speedInterface() {}
virtual int GetSpeed(void) = 0;
};
class Mock_SpeedInterface : public speedInterface
{
public:
MOCK_CONST_METHOD0(GetSpeed, int());
};
class Car
{
public:
Car(speedInterface& s) : Speedo(s) {}
virtual ~Car() {}
speedInterface& Speedo;
...
};
TEST(TestCar, Test1) {
Mock_SpeedInterface mockSpeed;
...
}
尝试创建 mockSpeed 会导致以下编译器错误:
Error C2259 'Mock_SpeedInterface': cannot instantiate abstract class
恕我直言 class Mock_SpeedInterface 不是抽象 class 因为它 "implements" GetSpeed。
为什么会出现此错误以及如何防止它?
似乎 MOCK_CONST_METHOD0(GetSpeed, int());
是错误的。为了得到一个普通的答案,我将引用 OP 的评论:
Changing it to MOCK_METHOD0 fix it.
我有一辆使用 speedInterface 的 class 汽车(上面有一个参考)。
现在我想用 Mock_SpeedInterface.
class speedInterface
{
public:
virtual ~speedInterface() {}
virtual int GetSpeed(void) = 0;
};
class Mock_SpeedInterface : public speedInterface
{
public:
MOCK_CONST_METHOD0(GetSpeed, int());
};
class Car
{
public:
Car(speedInterface& s) : Speedo(s) {}
virtual ~Car() {}
speedInterface& Speedo;
...
};
TEST(TestCar, Test1) {
Mock_SpeedInterface mockSpeed;
...
}
尝试创建 mockSpeed 会导致以下编译器错误:
Error C2259 'Mock_SpeedInterface': cannot instantiate abstract class
恕我直言 class Mock_SpeedInterface 不是抽象 class 因为它 "implements" GetSpeed。 为什么会出现此错误以及如何防止它?
似乎 MOCK_CONST_METHOD0(GetSpeed, int());
是错误的。为了得到一个普通的答案,我将引用 OP 的评论:
Changing it to MOCK_METHOD0 fix it.