其他分享
首页 > 其他分享> > c – 模板化模拟类中的MOCK_METHODx()定义缺少’typename’

c – 模板化模拟类中的MOCK_METHODx()定义缺少’typename’

作者:互联网

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

目的是测试框架支持的回调方法,但框架基类依赖于最终实现(简而言之,它是一个注入静态接口声明的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’

是否有可能以某种方式将ON_CALL()宏中使用的gmock Matcher专门用于模板参数?或者我错过了s.th.其他??

解决方法:

我想你需要附加了_T的gmock宏的模板版本:

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

有关详细信息,请参阅the docs中标题为“模拟类模板”的部分.

标签:c,gmock
来源: https://codeday.me/bug/20190901/1779761.html