Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Mock testing::internal::ReturnAction<R> array with negative size

Tags:

c++

googlemock

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?

like image 980
Alexander Torstling Avatar asked May 19 '15 10:05

Alexander Torstling


1 Answers

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));
like image 91
Alexander Torstling Avatar answered Oct 22 '22 11:10

Alexander Torstling