gpt4 book ai didi

c++ - 使用 gmock Matchers 将 std::function 设置为 EXPECT_CALL 中的方法参数

转载 作者:行者123 更新时间:2023-12-02 10:08:29 25 4
gpt4 key购买 nike

尝试对类的私有(private)回调方法进行单元测试

class MyClass{
public:
MyClass(Component& component) : component(component) {};
void Init();
private:
void callback();
Component component;
}

void MyClass::callback(const std::string& text)
{
...
}

使用我可以模拟的组件成员(component.register() 方法)在公共(public) Init 方法中注册回调。
void MyClass::Init(){
auto cb = std::bind(&MyClass::callback, this, std::placeholders::_1);
connection = component.register(ESomeType::MyType, std::move(cb));
}

MOCK_METHOD2(register, boost::signals2::connection(ESomeType, std::function<void(const std::string&)>));

正如这里建议的那样
How to unit test the std::bind function using gtest?
我想 EXPECT_CALL 的 component.register() 函数并使用 SaveArg 将传递的 std::function<> 参数存储在单元测试局部变量中。
然后使用该变量我应该能够调用回调进行测试。

由于 component.register() 是重载函数,我需要使用 Matchers 将确切的参数类型传递给 EXPECT_CALL 以避免歧义。

不幸的是,设置 Matcher 类型时遇到问题。

当前测试代码:
ComponentMock component;
MyClass testClass(component);

std::function <void(const std::string& text)> componentCallback;

EXPECT_CALL(component, register(Matcher<ESomeType>(ESomeType::MyType),
Matcher< void(const std::string& text) >(componentCallback)))
.WillOnce(testing::SaveArg<1>(&componentCallback;);

testClass.init();

testClass.componentCallback( ... );

这种方法首先是正确的吗?如果是的话,你能帮我解决以下错误吗:
In file included from gmock-generated-function-mockers.h:43:0, gmock.h:61, ComponentMock.hpp:16, Test.cpp:18:
In member function 'void Test_initialize()':
Test.cpp: error: no matching function for call to 'testing::Matcher<void(const std::basic_string<char>&)>::Matcher(std::function<void(const std::basic_string<char>&)>&)'
Matcher< void(const std::string& string) >(componentCallback)));
^
...
gmock-matchers.h:3747:1: note: candidate: testing::Matcher<T>::Matcher(T) [with T = void(const std::basic_string<char>&)]
Matcher<T>::Matcher(T value) { *this = Eq(value); }
^~~~~~~~~~
gmock-matchers.h:3747:1: note: no known conversion for argument 1 from 'std::function<void(const std::basic_string<char>&)>' to 'void (*)(const std::basic_string<char>&)'
...

最佳答案

是的,这通常是正确的方法,但是您发布的代码存在一些问题。首先,您为 Matcher 指定的类型不正确(您忘记了 std::function )。其次,std::function没有提供适当的相等运算符,因此您必须使用 ::testing::An而不是 ::testing::Matcher .

修复这些问题后,测试主体应如下所示:

ComponentMock component;
MyClass testClass(component);

std::function <void(const std::string& text)> componentCallback;

EXPECT_CALL(component, register(Matcher<ESomeType>(ESomeType::MyType),
An< std::function<void(const std::string& text)>>()))
.WillOnce(testing::SaveArg<1>(&componentCallback;);

testClass.init();

testClass.componentCallback( ... );

顺便说一句,避免使用 register作为函数的名称。

关于c++ - 使用 gmock Matchers 将 std::function 设置为 EXPECT_CALL 中的方法参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49389598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com