首页 > 解决方案 > 我怎么能模拟 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().

标签: cunit-testingtestingcmocka

解决方案


我想出了这个解决方案。

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()。我想知道这是否是最好的方法。


推荐阅读