I am mocking a class and would like to redirect calls ,but an error occured mean no matching function for call,code like this:
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;
}
when Compile this code, compiler show the error,no matching function for call to '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>&)>&'
You didn't mention GoogleTest version used, but passing a callable directly as action seems to be supported only since 1.10. Before that, you should use Invoke
function to create an action:
EXPECT_CALL(*cc_handler, GetSetAttInfo(::testing::_, ::testing::_))
.Times(1)
.WillOnce(::testing::Invoke(
[&](const string& business_direction, vector<SetAttrCfg>& set_attributes) {
set_attributes = MakeSetAttrCfg();
}
));
Side note: .Times(1)
adds nothing when paired with WillOnce
. When cardinality (.Times()
call) is omitted, GoogleMock will deduce number of calls from the .WillOnce()
/.WillRepeatedly()
calls.