gtest模拟错误没有匹配的调用函数
gtest mock error no matching function for call
我正在mock一个class,想重定向调用,但是出现错误意味着没有匹配的调用函数,代码如下:
class MockCCHandler : public CCHandler {
public:
MockCCHandler() {}
~MockCCHandler() override = default;
MOCK_METHOD0(GetIdcName, string());
MOCK_METHOD0(GetCurrentCityRT, City());
MOCK_METHOD2(GetSetAttInfo, void(const string& business_direction, vector<SetAttrCfg>& set_attributes));
MOCK_METHOD1(GetCkvConf, CkvCfgL5(const string&));
CCHandlerPtr GetMockPtr();
private:
vector<SetAttrCfg> MakeSetAttrCfg();
CkvCfgL5 MakeCkvCfgL5();
CkvCfgL5 MakeCrossCityCkvCfgL5();
};
CCHandlerPtr MockCCHandler::GetMockPtr() {
std::shared_ptr<MockCCHandler> cc_handler = std::make_shared<MockCCHandler>();
EXPECT_CALL(*cc_handler, GetIdcName())
.Times(1)
.WillOnce(::testing::Return("BX"));
EXPECT_CALL(*cc_handler, GetCurrentCityRT())
.Times(1)
.WillOnce(::testing::Return(City("Shenzhen")));
EXPECT_CALL(*cc_handler, GetSetAttInfo(::testing::_, ::testing::_))
.Times(1)
.WillOnce([&](const string& business_direction, vector<SetAttrCfg>& set_attributes) {
set_attributes = MakeSetAttrCfg();
});
EXPECT_CALL(*cc_handler, GetCkvConf(::testing::_))
.Times(2)
.WillOnce(::testing::Return(MakeCkvCfgL5()))
.WillOnce(::testing::Return(MakeCrossCityCkvCfgL5()));
return cc_handler;
}
当编译这段代码时,编译器显示错误,没有匹配的函数来调用 'testing::internal::TypedExpectation<void(const std::basic_string&, std::vector&)>::WillOnce(MockCCHandler::GetMockPtr()::__lambda0)'
});
mock/cc_handler_mock.cc:18:6:error: no matching function for call to 'testing::internal::TypedExpectation<void(const std::basic_string<char>&, std::vector<SetAttrCfg>&)>::WillOnce(MockCCHandler::GetMockPtr()::__lambda0)'
});
^
mock/cc_handler_mock.cc:18:6: note: candidate is:
In file included from gtest/include/gmock/gmock-generated-function-mockers.h:43:0,
from gtest/include/gmock/gmock.h:61,
from mock/cc_handler_mock.h:3,
from mock/cc_handler_mock.cc:1:
gtest/include/gmock/gmock-spec-builders.h:994:21: note: testing::internal::TypedExpectation<F>& testing::internal::TypedExpectation<F>::WillOnce(const testing::Action<F>&) [with F = void(const std::basic_string<char>&, std::vector<SetAttrCfg>&)]
TypedExpectation& WillOnce(const Action<F>& action) {
^
gtest/include/gmock/gmock-spec-builders.h:994:21: note: no known conversion for argument 1 from 'MockCCHandler::GetMockPtr()::__lambda0' to 'const testing::Action<void(const std::basic_string<char>&, std::vector<SetAttrCfg>&)>&'
您没有提到使用的 GoogleTest 版本,但是从 1.10 开始似乎只支持直接将可调用项作为操作传递。在此之前,您应该使用 Invoke
函数来创建一个动作:
EXPECT_CALL(*cc_handler, GetSetAttInfo(::testing::_, ::testing::_))
.Times(1)
.WillOnce(::testing::Invoke(
[&](const string& business_direction, vector<SetAttrCfg>& set_attributes) {
set_attributes = MakeSetAttrCfg();
}
));
旁注:.Times(1)
与 WillOnce
配对时不添加任何内容。当省略基数(.Times()
调用)时,GoogleMock 将从 .WillOnce()
/.WillRepeatedly()
调用中推断出调用次数。
我正在mock一个class,想重定向调用,但是出现错误意味着没有匹配的调用函数,代码如下:
class MockCCHandler : public CCHandler {
public:
MockCCHandler() {}
~MockCCHandler() override = default;
MOCK_METHOD0(GetIdcName, string());
MOCK_METHOD0(GetCurrentCityRT, City());
MOCK_METHOD2(GetSetAttInfo, void(const string& business_direction, vector<SetAttrCfg>& set_attributes));
MOCK_METHOD1(GetCkvConf, CkvCfgL5(const string&));
CCHandlerPtr GetMockPtr();
private:
vector<SetAttrCfg> MakeSetAttrCfg();
CkvCfgL5 MakeCkvCfgL5();
CkvCfgL5 MakeCrossCityCkvCfgL5();
};
CCHandlerPtr MockCCHandler::GetMockPtr() {
std::shared_ptr<MockCCHandler> cc_handler = std::make_shared<MockCCHandler>();
EXPECT_CALL(*cc_handler, GetIdcName())
.Times(1)
.WillOnce(::testing::Return("BX"));
EXPECT_CALL(*cc_handler, GetCurrentCityRT())
.Times(1)
.WillOnce(::testing::Return(City("Shenzhen")));
EXPECT_CALL(*cc_handler, GetSetAttInfo(::testing::_, ::testing::_))
.Times(1)
.WillOnce([&](const string& business_direction, vector<SetAttrCfg>& set_attributes) {
set_attributes = MakeSetAttrCfg();
});
EXPECT_CALL(*cc_handler, GetCkvConf(::testing::_))
.Times(2)
.WillOnce(::testing::Return(MakeCkvCfgL5()))
.WillOnce(::testing::Return(MakeCrossCityCkvCfgL5()));
return cc_handler;
}
当编译这段代码时,编译器显示错误,没有匹配的函数来调用 'testing::internal::TypedExpectation<void(const std::basic_string&, std::vector&)>::WillOnce(MockCCHandler::GetMockPtr()::__lambda0)' });
mock/cc_handler_mock.cc:18:6:error: no matching function for call to 'testing::internal::TypedExpectation<void(const std::basic_string<char>&, std::vector<SetAttrCfg>&)>::WillOnce(MockCCHandler::GetMockPtr()::__lambda0)'
});
^
mock/cc_handler_mock.cc:18:6: note: candidate is:
In file included from gtest/include/gmock/gmock-generated-function-mockers.h:43:0,
from gtest/include/gmock/gmock.h:61,
from mock/cc_handler_mock.h:3,
from mock/cc_handler_mock.cc:1:
gtest/include/gmock/gmock-spec-builders.h:994:21: note: testing::internal::TypedExpectation<F>& testing::internal::TypedExpectation<F>::WillOnce(const testing::Action<F>&) [with F = void(const std::basic_string<char>&, std::vector<SetAttrCfg>&)]
TypedExpectation& WillOnce(const Action<F>& action) {
^
gtest/include/gmock/gmock-spec-builders.h:994:21: note: no known conversion for argument 1 from 'MockCCHandler::GetMockPtr()::__lambda0' to 'const testing::Action<void(const std::basic_string<char>&, std::vector<SetAttrCfg>&)>&'
您没有提到使用的 GoogleTest 版本,但是从 1.10 开始似乎只支持直接将可调用项作为操作传递。在此之前,您应该使用 Invoke
函数来创建一个动作:
EXPECT_CALL(*cc_handler, GetSetAttInfo(::testing::_, ::testing::_))
.Times(1)
.WillOnce(::testing::Invoke(
[&](const string& business_direction, vector<SetAttrCfg>& set_attributes) {
set_attributes = MakeSetAttrCfg();
}
));
旁注:.Times(1)
与 WillOnce
配对时不添加任何内容。当省略基数(.Times()
调用)时,GoogleMock 将从 .WillOnce()
/.WillRepeatedly()
调用中推断出调用次数。