google mock 无法模拟带有模板化参数的方法

google mock unable to mock a method with a templated argument

我不确定我正在尝试做的事情是否可行,但我很难让编译器尝试模拟包含 模板化参考参数 的方法。

接口(删除了所有不相关的方法)

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

我的模拟

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

这导致

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’

我的 C++ 技能不够好,无法了解编译器试图告诉我的内容。

谁能帮帮我?

嗯,这很奇怪,但是简单 using 可以解决您的问题。

#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));
};

来自 gMock 食谱 Dealing with unprotected commas

最新的 gmock 库显示了更具描述性的内容:

error: static_assert failed due to requirement '::testing::tuple_size<std::tuple<etl::vector<network_operator, 5> &>>::value == 2' "This method does not take 2 arguments. Parenthesize all types with unproctected commas."
    MOCK_METHOD(void, get_available_operators,

Parenthesize all types with unproctected commas.

没有using

的另一种解决方案
#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(etl::vector<network_operator, 5>&) = 0;
};

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