Search code examples
c++unit-testinggoogletest

google mock unable to mock a method with a templated argument


I am not sure if what I am trying to do is possible, but I have a hard time with the compiler trying to mock a method which contains a templated reference parameter.

The interface (removed all irrelevant methods)

class iat_protocol
{
public:
    virtual void get_available_operators(etl::vector<network_operator, 5>&) = 0;
};

My mock

class at_protocol_mock : public iat_protocol
{
public:
    MOCK_METHOD((void), get_available_operators, (etl::vector<network_operator, 5>&), (override));
};

This results in

In file included from /home/bp/dev/unode/eclipse/thirdparty/googletest/googlemock/include/gmock/gmock-actions.h:145,
                 from /home/bp/dev/unode/eclipse/thirdparty/googletest/googlemock/include/gmock/gmock.h:57,
                 from ../tests/shared/hal/at/at_channel_tests.cpp:1: /home/bp/dev/unode/eclipse/unit_tests/tests/shared/hal/at/mocks/at_protocol_mock.hpp: In member function ‘testing::internal::MockSpec<void(etl::vector<iobox::hal::at::network_operator, 5>&)> iobox::hal::at_protocol_mock::gmock_get_available_operators(const testing::internal::WithoutMatchers&, testing::internal::Function<void(etl::vector<iobox::hal::at::network_operator, 5>&)>*) const’: /home/bp/dev/unode/eclipse/thirdparty/googletest/googlemock/include/gmock/gmock-function-mocker.h:343:74: error: invalid combination of multiple type-specifiers   343 |   typename ::testing::internal::Function<__VA_ARGS__>::template Arg<_i>::type
      |                                                                          ^~~~ /home/bp/dev/unode/eclipse/thirdparty/googletest/googlemock/include/gmock/internal/gmock-pp.h:17:31: note: in definition of macro ‘GMOCK_PP_IDENTITY’

My c++ skills are not good enough to have a clue what the compiler tries to tell me.

Who can help me ?


Solution

  • Well, this is strange, but simple using fixes your problem.

    #include "gmock/gmock.h"
    
    struct network_operator {};
    
    namespace etl {
    template <typename T, unsigned N>
    struct vector {};
    }  // namespace etl
    
    using vector_5 = etl::vector<network_operator, 5>;
    
    class iat_protocol {
       public:
        virtual void get_available_operators(vector_5&) = 0;
    };
    
    class at_protocol_mock : public iat_protocol {
       public:
        MOCK_METHOD(void, get_available_operators,
                    (vector_5&),(override));
    };
    
    

    From gMock cookbook Dealing with unprotected commas