我有一个 gmock 和模板化模拟类的编译器错误问题,该类应该用作派生(具体)模拟类的基础。
目的是测试框架支持的回调方法,但框架基类依赖于最终的实现(简而言之,它是一个注入(inject)静态接口(interface)声明的 CRTP 模式样式框架)-
我试图勾勒出我所拥有的(请不要在第一次尝试中依赖可编译的代码):
这是依赖于 Context 模板参数的框架钩子(Hook)接口定义,框架基类本身将其作为非多态调用处理并提供默认实现:
template<class Context>
class IFrameworkHooks
{
public:
virtual void funcImpl(Context* context) = 0;
virtual ~IFrameworkHooks() {}
};
现在我想实现一个实现
IFrameWorkHooks<>
接口(interface)的模拟类: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 专门用于模板参数?还是我想念某些东西?别的?? 最佳答案
我认为您需要附加 _T
的 gmock 宏的模板版本:
MOCK_METHOD1_T(funcImpl, void (Context* context));
有关更多信息,请参阅 the docs 中标题为“模拟类模板”的部分。
关于c++ - 模板模拟类中的 MOCK_METHOD() 定义缺少“typename”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16244559/