【问题标题】:googlemock: object of mocked class cannot be createdgooglemock:无法创建模拟类的对象
【发布时间】:2018-03-16 09:53:02
【问题描述】:

我有一个使用 speedInterface 的类 Car(它有一个参考)。
现在我想用 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 会导致以下编译器错误:

错误 C2259 'Mock_SpeedInterface':无法实例化抽象类

恕我直言,Mock_SpeedInterface 类不是抽象类,因为它“实现”GetSpeed。 为什么会出现此错误,如何防止?

【问题讨论】:

  • 你为什么用MOCK_CONST_METHOD0代替GetSpeed,因为这个方法是non-const。你应该使用MOCK_METHOD0
  • 哦。你是对的。我没看到。将其更改为 MOCK_METHOD0 修复它。非常感谢。

标签: c++ googlemock


【解决方案1】:

似乎MOCK_CONST_METHOD0(GetSpeed, int()); 是错误的。为了得到一个普通的答案,我将引用 OP 的评论:

将其更改为 MOCK_METHOD0 修复它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    相关资源
    最近更新 更多