使用GoogleMock指定输出字符串参数

evr*_*rn0 4 c unit-testing googletest googlemock

我正在将Google Test / Mock评估为C代码单元测试的框架。

如何为我要模拟的函数指定输出字符串参数?

这里有int get_int_param(const char *)要测试的函数,它使用int _get_text_file_content(const char *fn, char *content)了我想模拟的函数。

如何指定char *content将要执行模拟功能的结果?

我在这段代码中苦苦挣扎:

TEST(GetParameterTest,Positiv){
   const static int strLen=29;
   char *text=(char *)calloc(strLen,1);
   strcpy(text, "param1=1\nparam2=42\nparam3=3");

   MokedFunctions mokedFunctions;
   EXPECT_CALL(mokedFunctions, _get_text_file_content("process.conf",_)).Times(AtLeast(1)).WillOnce(SetArgReferee<1>(text));

   EXPECT_EQ(1, get_int_param("param1"));
}
Run Code Online (Sandbox Code Playgroud)

并得到此编译错误:

/usr/include/gmock/gmock-more-actions.h: In instantiation of ‘typename 
testing::internal::Function<F>::Result testing::SetArgRefereeActionP<k, 
value_type>::gmock_Impl<F>::gmock_PerformImpl(const args_type&, 
arg0_type, arg1_type, arg2_type, arg3_type, arg4_type, arg5_type, 
arg6_type, arg7_type, arg8_type, arg9_type) const [with arg0_type = 
const char*; arg1_type = char*; 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 = int(const char*, char*); int k = 1; 
value_type = char*; typename testing::internal::Function<F>::Result = 
int; testing::SetArgRefereeActionP<k, 
value_type>::gmock_Impl<F>::args_type = std::tuple<const char*, char*>]’:

 /usr/include/gmock/gmock-generated-actions.h:664:23:   required from 
 ‘static Result testing::internal::ActionHelper<Result, 
 Impl>::Perform(Impl*, const std::tuple<_U1, _U2>&) [with A0 = const 
 char*; A1 = char*; Result = int; Impl = 
 testing::SetArgRefereeActionP<1, char*>::gmock_Impl<int(const char*, 
 char*)>]’

 /usr/include/gmock/gmock-more-actions.h:168:1:   required from 
 ‘testing::SetArgRefereeActionP<k, 
 value_type>::gmock_Impl<F>::return_type 
 testing::SetArgRefereeActionP<k, 
 value_type>::gmock_Impl<F>::Perform(const args_type&) [with F = 
 int(const char*, char*); int k = 1; value_type = char*; 
 testing::SetArgRefereeActionP<k, 
 value_type>::gmock_Impl<F>::return_type = int; 
 testing::SetArgRefereeActionP<k, value_type>::gmock_Impl<F>::args_type 
 = std::tuple<const char*, char*>]’

 test_param.cpp:68:1:   required from here
 /usr/include/gmock/gmock-more-actions.h:175:3: error: size of array is negative
 GTEST_COMPILE_ASSERT_(internal::is_reference<argk_type>::value,
 ^
 In file included from /usr/include/gmock/gmock.h:65:0,
             from test_param.cpp:2:
  /usr/include/gmock/gmock-more-actions.h:177:28: error: assignment of read-only location ‘std::get<1u, {const char*, char*}>((* & args))’
    ::std::tr1::get<k>(args) = value;
                        ^
  make[1]: *** [test_param.o] Error 1
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

Vla*_*sev 6

SetArgReferee 期望参数是C ++引用,而不是您的情况。

通常,为了更好地理解这些动作,有助于将它们视为对参数的操作arg

  • SetArgPointee(value)本质上*arg = valuearg必须是一个指针)
  • SetArgReferee(value)arg = valuearg必须作为参考)
  • SetArrayArgument(first, last)memcpy(arg, first, last - first)arg必须是一个指针)
  • SaveArg(ptr)*ptr = arg
  • SaveArgPointee(ptr)*ptr = *argarg必须是一个指针)

鉴于此,您显然需要采取的行动是SetArrayArgument<1>(text, text + strlen(text) + 1)


ros*_*031 0

您正在使用 C++ 测试框架来测试 C 代码。除非您愿意使用 g++(而不是 gcc)编译代码,否则它不会工作。Gcc 无法编译 C++ 代码。