5

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

有一些类定义,可以说:

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

如您所见,method_a不是vitual 。

我可以编码吗

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

没有黑暗的结果?

更深入。我可以在 MockA 中覆盖method_a吗?

像:

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

1 回答 1

3

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

根据在不破坏现有代码的情况下简化界面。来自 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));
};

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

于 2013-11-28T13:21:19.467 回答