c++ - GMock : Has to be always pure virtual the base class of a mock class?

标签 c++ googlemock

我已经阅读了 googlecode 中的良好做法。他们是对的,但我仍然对以下内容感到好奇:

有一些类定义,假设:

class A{
   virtual void method_a(){}
};

如您所见,method_a 不是纯虚拟的

我可以写代码吗

class MockA: public A{
    MOCK_METHOD(method_a, void());
};

没有暗色结果?

更进一步。我可以覆盖 MockA 中的 method_a 吗?

喜欢:

class MockA: public A{
    void method_a(){
        // Do something here.
    }
};

最佳答案

好吧,我刚刚做了一个测试,看来我们可以。我正在使用这种方法来测试一些参数超过 10 个的类函数。

根据 Simplifying the Interface without Breaking Existing Code .来自 gmock 食谱。

例如:

class SomeClass {
     ...
     virtual void bad_designed_func(int a, ...){ // This functions has up to 12 parameters.
                                                 // The others were omitted for simplicity.
};


class MockSomeClass: public SomeClass {
    ...
    void bad_designed_func(int a, ...){ // This functions recives 12 parameters.
                                        // The others were omitted for simplicity.
        ...   
        test_wat_i_want(a);  // Mock method call. I'm only interest in paramater a.
    }

    MOCK_METHOD1(test_wat_i_want, void(int a));
};

在我的代码中,我没有任何抽象类(意味着根本没有纯虚函数)。 这不是推荐的方法,但有助于我们处理遗留代码。

关于c++ - GMock : Has to be always pure virtual the base class of a mock class?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20226619/

相关文章:

c++ - 使用 Google Mocks,如何在不关心/设置任何调用期望的情况下给出模拟实现

c++ - 谷歌模拟 :Testing a Certain Property of an Object

c++ - 错误: Initialization with '{...}' expected for aggregate object

c++ - GTest测试用例 "EXPECT_CALL"编译错误

c++ - QTextEdit 插入和删除行非常慢。无论如何让它更快?

C++ 为什么我要使用#define 而不是内联?

c++ - Google模拟和替代关键字

c++ - gmock 忽略 "interesting"函数调用

c++ - 模板参数是如何展开的

c++ - 通过无序集访问类数据不能正常工作?