c - 在 CMock 中使用 ExpectWithArray 的示例

标签 c unit-testing cmock

我在 Ubuntu 16.04 和 Eclipse 4.7.2 下使用 Ceedling。到目前为止,一切正常,但我无法使 _ExpectWithArray 模拟函数正常工作。

例如,我有以下需要模拟的函数 void TestFunc(uint8_t * data);。在我的测试文件中,我有以下调用 uint8_t TEST_DATA[5] = { 0xFF, 0x00, 0xA0, 0x00, 0x09 }; TestFunc_ExpectWithArray(TEST_DATA, 5)

我也尝试为 param_depth 提供不同的值,但没有成功。

当我尝试运行测试时,它总是失败并显示

implicit declaration of function ‘TestFunc_ExpectWithArray’ [-Wimplicit-function-declaration]

根据我的经验,当未使用正确的参数调用要模拟的函数并且 CMock 无法生成模拟版本时,总是会发生这种情况。我究竟做错了什么?有人可以举例说明如何正确使用 _ExpectWithArray 吗?

最佳答案

为 .yml 文件中的插件添加此行 - :array

    :cmock:
    :mock_prefix: mock_
    :when_no_prototypes: :warn
    :enforce_strict_ordering: TRUE
    :plugins:
       - :array
       - :ignore
       - :callback

示例使用 _ExpectWithArray

/test/test_example.c

    #include "unity.h"
    #include "temp.h"
    #include "mock_example.h"

    void setUp(void)
    {
    }

    void tearDown(void)
    {
    }

    void test_sendMesFirst(void)
    {
        uint8_t message[] = {"Hello"}, answerMessage[] = {"Hello"}, answerNum = 4;
        sendBytes_ExpectWithArray(answerMessage, sizeof(message), answerNum);
        sendMes(message, sizeof(message), answerNum);
    }  

/src/example.h

   #ifndef example_H
   #define example_H

   #include "stdint.h"

   void sendBytes(uint8_t *bytes, int size);

   #endif //

/src/temp.c

    #include "temp.h"
    #include "example.h"


    void sendMes(uint8_t *mes, int size, int num)
    {
        if(num < size)
            sendBytes(mes, num);
        else
            sendBytes(mes, size);
    }

/src/temp.h

    #ifndef temp_H
    #define temp_H

    #include "stdint.h"

    void sendMes(uint8_t *mes, int size, int num);        
    #endif

关于c - 在 CMock 中使用 ExpectWithArray 的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49172343/

相关文章:

c - __attribute__((weak) ) 函数导致 undefined reference

c - P线程介绍

java - maven测试报告格式

ARM STM32 的 Ceedling CMock 设置

javascript - 如何对 Angular Directive(指令)进行单元测试

ios - 在运行时或通过测试用例完成的静态数据验证

c - 如何将CMock单元测试框架与Make集成

c - 将两个值保存为数组中的一个索引,以便我可以使用特定索引来调用这些值对

c - 如何在for循环中使用scanf

c++ - const typedef 之间的区别