πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

'typename' missing for MOCK_METHODx() definitions in templated mock class

I have a compiler error issue with gmock and a templated mock class that should be used as base for derived (concrete) mock classes.

The purpose is to test callback methods supported by a framework but the framework base classes are dependent on the final implementation (in short it's a CRTP pattern style framework that injects static interface declarations)-

I'm trying to sketch out what I have (please don't rely on compilable code in the 1st try):

This is the framework hooks interface definition that depends on a Context template parameter, the framework base class itself handles this as a non polymorphic call and provides a default implementation:

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

Now I want to implement a mock class that implements the 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)
    {
    }

};

I'm getting a compiler error that says:

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’

Is it possible to specialize the gmock Matcher used in the ON_CALL() macro for the template parameter somehow? Or am I missing s.th. else??

Upvotes: 3

Views: 1451

Answers (1)

Fraser
Fraser

Reputation: 78330

I think you need the template version of gmock's macros which have _T appended:

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

For further info, see the section entitled "Mocking Class Templates" in the docs.

Upvotes: 6

Related Questions