c++ - 谷歌模拟无法模拟带有模板化参数的方法

标签 c++ unit-testing googletest

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

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

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

关于c++ - 谷歌模拟无法模拟带有模板化参数的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71538502/

相关文章:

c++ - Arduino EEPROM 获取损坏的值

c++ - 专门化采用通用引用参数的函数模板

unit-testing - 模拟属性 setter

c++ - 将函数参数从 EXPECT_CALL 保存到模拟方法

c++ - 当不需要检查缩小时,是否需要实例化函数定义?

c++ - 将批处理脚本作为 Windows 服务运行

java - 如何测试到期条件?

unit-testing - Spock - @Subject 注释和实用程序 |正在测试的辅助类

c++ - googletest:如何将所有测试包含在一个可执行文件中?

c++ - 如何使用 Google Test 测试 EXE? (2)