c - 我怎么能模拟 void 类型函数?
问题描述
我有一个 void insert 函数,我想测试它是否正确插入。我正在使用 CMocka 框架进行测试。
我试图dpl_insert(ret, "mock_value")
代替,will_return()
但似乎没有值附加到列表中。
t_list **dpl_create();
int ft_list_size(t_list *self);
void __wrap_dpl_insert(t_list **self, void *data)
{
check_expected(self);
check_expected(data);
}
static void test_dpl_insert_empty_list(void **state)
{
(void) state;
t_list **ret = dpl_create(); //Initialization of an empty struct.
int ret_size;
expect_value(__wrap_dpl_insert, self, ret);
expect_value(__wrap_dpl_insert, data, "mock_value");
will_return(__wrap_dpl_insert, XXX); //PROBLEM RESIDES HERE.
ret_size = ft_list_size(*ret);
fprintf(stderr, "Size of the list=>%d\n", ret_size);
}
dpl_insert(ret, "mock_value")
打印 0 ,fprintf()
就像没有添加元素一样。
我的目标是从fprintf()
.
解决方案
我想出了这个解决方案。
static void test_dpl_insert_empty_list(void **state)
{
(void) state; /* unused */
t_list **ret;
t_list *beg;
int count, data;
count = 0;
will_return(__wrap_dpl_create, malloc(sizeof(t_list)));
ret = dpl_create();
dpl_insert(ret, (void*)1);
dpl_insert(ret, (void*)2);
dpl_insert(ret, (void*)3);
beg = *ret;
while(beg != NULL)
{
data = (intptr_t)beg->data;
assert_int_equal(++count, data);
beg = beg->next;
}
}
而不是使用__wrap_dpl_insert()
我使用原始功能dpl_insert()
。我想知道这是否是最好的方法。
推荐阅读
- angular - 未拾取 ActivatedRoute 参数
- c# - 对“MarshalByRefObject”类型的引用声称它是在“System.Runtime”中定义的,但找不到
- aws-lambda - 失败:ValidationException:检测到 1 个验证错误:“tableName”处的值“[object Object]”未能满足约束
- jwt - 如何将用户添加到 Zoom 帐户的 JWT 密钥?
- google-cloud-platform - 如何参数化 Kubeflow Pipelines 环境变量?
- html - 我的网站 CSS 动画有效,但之后链接不起作用
- linux - 如何检测我是否在 Linux 中的 Electron AppImage 中运行?
- java - Keycloak Federation Configuration,初始化服务时出现keystore/truststore错误
- python - python3使用模块内部的内部依赖解决相对导入
- r - 如何根据 r 的年增长率将新列添加到数据框中?