首页 > 解决方案 > 谷歌测试:寻找一个工作示例,将附加参数传递给被调用的函数

问题描述

我正在关注gMock Cookbook中的示例。我在编译它时遇到问题。如果您有一个将附加参数传递给调用函数的工作示例,如果您能分享一个链接,我将不胜感激。

我将所有代码放在“TEST_F(FooTest,Test)”声明中的“...”下。我将其余代码放在*.h 文件中,根据MockFoo 中的代码添加一个具有两个虚函数的类Foo。我添加了一个从 testing::Test 中提取的类 FooTest。我在 gtest.h 和 gmock.h 中添加了 include 语句。


我使用 g++ 9.3.0 来构建我的代码。
构建会导致以下编译器错误。

In file included from /home/gidi/temp/gtest/googletest-master/googlemock/include/gmock/gmock-actions.h:145,
                 from /home/gidi/temp/gtest/googletest-master/googlemock/include/gmock/gmock.h:57,
                 from Test.h:2,
                 from Test.cpp:1:
Test.h:14:20: error: ‘testing::internal::Function<int(int, int)>::Result MockFoo::Sum(testing::internal::ElemFromList<0, int, int>::type, testing::internal::ElemFromList<1, int, int>::type)’ marked ‘override’, but does not override
   14 |   MOCK_METHOD(int, Sum, (int x, int y), (override));
      |                    ^~~
/home/gidi/temp/gtest/googletest-master/googlemock/include/gmock/internal/gmock-pp.h:17:31: note: in definition of macro ‘GMOCK_PP_IDENTITY’
   17 | #define GMOCK_PP_IDENTITY(_1) _1
      |                               ^~
/home/gidi/temp/gtest/googletest-master/googlemock/include/gmock/gmock-function-mocker.h:93:3: note: in expansion of macro ‘GMOCK_INTERNAL_MOCK_METHOD_IMPL’
   93 |   GMOCK_INTERNAL_MOCK_METHOD_IMPL(                                            \
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/gidi/temp/gtest/googletest-master/googlemock/include/gmock/internal/gmock-pp.h:152:39: note: in expansion of macro ‘GMOCK_INTERNAL_MOCK_METHOD_ARG_4’
  152 | #define GMOCK_PP_INTERNAL_CAT(_1, _2) _1##_2
      |                                       ^~
/home/gidi/temp/gtest/googletest-master/googlemock/include/gmock/gmock-function-mocker.h:76:3: note: in expansion of macro ‘GMOCK_PP_VARIADIC_CALL’
   76 |   GMOCK_PP_VARIADIC_CALL(GMOCK_INTERNAL_MOCK_METHOD_ARG_, __VA_ARGS__)
      |   ^~~~~~~~~~~~~~~~~~~~~~
Test.h:14:3: note: in expansion of macro ‘MOCK_METHOD’
   14 |   MOCK_METHOD(int, Sum, (int x, int y), (override));
      |   ^~~~~~~~~~~
Test.h:15:21: error: ‘testing::internal::Function<bool(int)>::Result MockFoo::ComplexJob(testing::internal::ElemFromList<0, int>::type)’ marked ‘override’, but does not override
   15 |   MOCK_METHOD(bool, ComplexJob, (int x), (override));
      |                     ^~~~~~~~~~
/home/gidi/temp/gtest/googletest-master/googlemock/include/gmock/internal/gmock-pp.h:17:31: note: in definition of macro ‘GMOCK_PP_IDENTITY’
   17 | #define GMOCK_PP_IDENTITY(_1) _1
      |                               ^~
/home/gidi/temp/gtest/googletest-master/googlemock/include/gmock/gmock-function-mocker.h:93:3: note: in expansion of macro ‘GMOCK_INTERNAL_MOCK_METHOD_IMPL’
   93 |   GMOCK_INTERNAL_MOCK_METHOD_IMPL(                                            \
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/gidi/temp/gtest/googletest-master/googlemock/include/gmock/internal/gmock-pp.h:152:39: note: in expansion of macro ‘GMOCK_INTERNAL_MOCK_METHOD_ARG_4’
  152 | #define GMOCK_PP_INTERNAL_CAT(_1, _2) _1##_2
      |                                       ^~
/home/gidi/temp/gtest/googletest-master/googlemock/include/gmock/gmock-function-mocker.h:76:3: note: in expansion of macro ‘GMOCK_PP_VARIADIC_CALL’
   76 |   GMOCK_PP_VARIADIC_CALL(GMOCK_INTERNAL_MOCK_METHOD_ARG_, __VA_ARGS__)
      |   ^~~~~~~~~~~~~~~~~~~~~~
Test.h:15:3: note: in expansion of macro ‘MOCK_METHOD’
   15 |   MOCK_METHOD(bool, ComplexJob, (int x), (override));
      |   ^~~~~~~~~~~
Test.cpp: In member function ‘virtual void FooTest_Test_Test::TestBody()’:
Test.cpp:11:30: error: ‘NewPermanentCallback’ was not declared in this scope
   11 |       .WillRepeatedly(Invoke(NewPermanentCallback(Sum3, 1)));
      |                              ^~~~~~~~~~~~~~~~~~~~
make: *** [Makefile:20: Test.o] Error 1

标签: googletest

解决方案


推荐阅读