I have testing code which does something like
EXPECT_CALL(mock, getSomeString()).WillOnce(Return(&testString));
where getSomeString()
is returning by reference:
std:string& getSomeString();
and get
../../../../src/test/unit/gmock/gmock-actions.h: In member function ‘testing::internal::ReturnAction<R>::operator testing::Action<Func>() const [with F = const std::string&(), R = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]’:
../../../../src/test/unit/MyTests.cc:148: instantiated from here
../../../../src/test/unit/gmock/gmock-actions.h:467: error: creating array with negative size (‘-0x00000000000000001’)
What is the cause?
Check gmock-actions.h:467 and you'll see:
GMOCK_COMPILE_ASSERT_(
!internal::is_reference<Result>::value,
use_ReturnRef_instead_of_Return_to_return_a_reference);
So the answer is to use ReturnRef
instead of Return
:
EXPECT_CALL(mock, getSomeString()).WillOnce(ReturnRef(testString));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With