谷歌模拟测试:: internal :: ReturnAction <r>数组负大小

时间:2015-05-19 10:26:49

标签: c++ googlemock

我有测试代码,类似

EXPECT_CALL(mock, getSomeString()).WillOnce(Return(&testString));

其中getSomeString()通过引用返回:

std:string& getSomeString();

并获取

../../../../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’)

原因是什么?

1 个答案:

答案 0 :(得分:16)

检查gmock-actions.h:467,你会看到:

GMOCK_COMPILE_ASSERT_(
        !internal::is_reference<Result>::value,
        use_ReturnRef_instead_of_Return_to_return_a_reference);

所以答案是使用ReturnRef代替Return

EXPECT_CALL(mock, getSomeString()).WillOnce(ReturnRef(testString));