我已经阅读了 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/