我有一個gmock和一個模板模擬類應該用作派生(具體)模擬類的基礎的編譯器錯誤問題。'typename'模板模擬類中的MOCK_METHODx()定義缺少
的目的是測試由一個框架,但該框架基類是依賴於最終實現(總之它是一個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匹配器以某種方式在模板參數的ON_CALL()
宏中使用?或者我錯過了s.th.其他??
你有沒有嘗試在'testing :: internal :: Function :: Result'?這就是錯誤信息說你需要 - 也許是正確的。 (您可能需要查看預處理器輸出以找出源代碼中的位置。) –
2013-04-26 20:24:50
@DavidSchwartz很難做到,因爲它被埋在了'MOCK_METHODx()'的gmocks實現中...... – 2013-04-26 20:38:06