3

我有一个 gmock 和一个模板模拟类的编译器错误问题,它应该用作派生(具体)模拟类的基础。

目的是测试框架支持的回调方法,但框架基类依赖于最终实现(简而言之,它是一个注入静态接口声明的 CRTP 模式样式框架)-

我正在尝试勾勒出我拥有的东西(请不要在第一次尝试中依赖可编译的代码):

这是依赖于 Context 模板参数的框架钩子接​​口定义,框架基类本身将其作为非多态调用处理并提供默认实现:

template<class Context>
class IFrameworkHooks
{
public:
    virtual void funcImpl(Context* context) = 0;
    virtual ~IFrameworkHooks() {}
};

现在我想实现一个实现IFrameWorkHooks<>接口的模拟类:

template<class Context, class InnerInterface>
class MyTemplateMock
: public FrameworkBaseClass<MyTemplateMock<Context,InnerInterface>,Context,InnerInterface>
, public IFrameworkHooks<Context>
{
public:
    // Compiler error here:
    MOCK_METHOD1(funcImpl, void (Context* context));
    virtual ~MyTemplateMock() {}

protected:
    MyTemplateMock()
    {
        // Compiler error here:
        ON_CALL(*this, funcImpl(_))
            .WillByDefault(Invoke(this, &MyTemplateMock<Context,InnerInterface>::funcImplCall));
    }

    void funcImplCall(Context* context)
    {
    }

};

我收到一个编译器错误,上面写着:

error: need ‘typename’ before ‘testing::internal::Function<void(Context*)>::Result’ because ‘testing::internal::Function<void(Context*)>’ is a dependent scope
error: type/value mismatch at argument 1 in template parameter list for ‘template<class T> class testing::Matcher’
error:   expected a type, got ‘testing::internal::Function<void(Context*)>::Argument1’

是否可以以某种方式将宏中使用的 gmock Matcher 专门用于ON_CALL()模板参数?或者我错过了什么。别的??

4

1 回答 1

7

我认为您需要_T附加的 gmock 宏的模板版本:

MOCK_METHOD1_T(funcImpl, void (Context* context));

有关详细信息,请参阅文档中标题为“模拟类模板”的部分。

于 2013-04-26T22:51:48.103 回答