【问题标题】:'typename' missing for MOCK_METHODx() definitions in templated mock class模板模拟类中的 MOCK_METHODx() 定义缺少“typename”
【发布时间】:2013-04-26 20:18:30
【问题描述】:

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

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

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

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

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

现在我想实现一个实现IFrameWorkHooks&lt;&gt;接口的模拟类:

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 专门用于模板参数?或者我错过了什么。别的??

【问题讨论】:

  • 您是否尝试将typename 放在testing::internal::Function&lt;void(Context*)&gt;::Result 之前?这就是错误消息所说的你需要的——也许它是正确的。 (您可能需要查看预处理器的输出以确定源代码中的位置。)
  • @DavidSchwartz 很难做到,因为这隐藏在 MOCK_METHODx() 的 gmocks 实现中......

标签: c++ gmock


【解决方案1】:

我认为您需要 gmock 宏的模板版本,其中附加了_T

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多