gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:26:16 25 4
gpt4 key购买 nike

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

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

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

如何指定这个 char *content 将成为 mocking 函数执行的结果?

我正在为这段代码苦苦挣扎:

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"));
}

得到这个编译错误:

/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

我做错了什么?

最佳答案

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

一般来说,为了更好地理解这些操作,将它们视为对参数 arg 的操作会有所帮助。 :

  • SetArgPointee(value)本质上是 *arg = value (arg 必须是指针)
  • SetArgReferee(value)arg = value (arg 必须是引用)
  • SetArrayArgument(first, last)memcpy(arg, first, last - first) (arg 必须是指针)
  • SaveArg(ptr)*ptr = arg
  • SaveArgPointee(ptr)*ptr = *arg (arg 必须是指针)

鉴于此,很明显您需要的操作是 SetArrayArgument<1>(text, text + strlen(text) + 1) .

关于c - 使用 GoogleMock 指定输出字符串参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30186405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com