在google test / mock框架中引用数组参数

时间:2014-02-18 07:53:05

标签: c++ arrays reference googletest googlemock

我有一个成员函数,引用了数组输出参数,我想在谷歌模拟中使用它,但它不起作用。

班级:

class Class
{
  // returns: number of the rewritten elements in the array
  int foo(Struct (&bar)[ArraySize]) const;
};

模拟课程:

class MockClass : public Class
{
  MOCK_CONST_METHOD1(foo, int(Struct (&)[ArraySize]));
};

当我想使用它时,我写下以下内容:

ON_CALL(mMockClass, foo(_))
  .WillByDefault( DoAll( SetArgReferee<0>(mBar)
                       , Return(5)
                       )
                );

mBar是一个合适的Struct(Struct mBar[ArraySize];)数组。当我编译它时,我收到以下错误消息:

../../../../vendor/gmock-1.7.0/include/gmock/gmock-more-actions.h: In member
function ‘typename testing::internal::Function<F>::Result
testing::SetArgRefereeActionP<k,
value_type>::gmock_Impl<F>::gmock_PerformImpl(const typename
testing::internal::Function<F>::ArgumentTuple&, arg0_type, arg1_type,
arg2_type, arg3_type, arg4_type, arg5_type, arg6_type, arg7_type, arg8_type,
arg9_type) const [with arg0_type = Struct (&)[32], arg1_type =
testing::internal::ExcessiveArg, arg2_type = testing::internal::ExcessiveArg,
arg3_type = testing::internal::ExcessiveArg, arg4_type =
testing::internal::ExcessiveArg, arg5_type = testing::internal::ExcessiveArg,
arg6_type = testing::internal::ExcessiveArg, arg7_type =
testing::internal::ExcessiveArg, arg8_type = testing::internal::ExcessiveArg,
arg9_type = testing::internal::ExcessiveArg, F = void(Struct (&)[32]), int k =
0, value_type = Struct*]’:
../../../../vendor/gmock-1.7.0/include/gmock/gmock-generated-actions.h:655:
instantiated from ‘static Result testing::internal::ActionHelper<Result,
Impl>::Perform(Impl*, const std::tr1::tuple<A0>&) [with A0 = Struct (&)[32],
Result = void, Impl = testing::SetArgRefereeActionP<0,
Struct*>::gmock_Impl<void(Struct (&)[32])>]’
../../../../vendor/gmock-1.7.0/include/gmock/gmock-more-actions.h:170:
instantiated from ‘typename testing::internal::Function<F>::Result
testing::SetArgRefereeActionP<k, value_type>::gmock_Impl<F>::Perform(const
typename testing::internal::Function<F>::ArgumentTuple&) [with F = void(Struct
(&)[32]), int k = 0, value_type = Struct*]’
SomeTest.cpp:120:   instantiated from here
../../../../vendor/gmock-1.7.0/include/gmock/gmock-more-actions.h:177: error:
incompatible types in assignment of ‘Struct* const’ to ‘Struct [32]’
make: *** [SomeTest] Error 1

我很抱歉错误消息格式。据我所知,编译器的问题是它假设对数组的引用但它得到一个指针。我错了什么?

1 个答案:

答案 0 :(得分:2)

更新:解决方案

对于传递类型数组SetArrayArgument<N>(first, last)的参数应该使用。

  

将源范围[first,last]中的元素复制到指向的数组   通过第N个(从0开始)的参数,它可以是指针或   迭代器。该操作不会获取所有元素的所有权   来源范围

有关Google C ++ Mocking Framework的更多信息[此处] [1]

http://code.google.com/p/googlemock/wiki/CheatSheet