unit-testingembeddedcmocka

Mocking functions with cmocka


I'm trying to mock some functions using cmocka:

void test_led_driver_NeedToImplement(void **state)
{
       
    state_t current = CLEAR;
    will_return(led_status_toggel,SET);
    
    TEST_ASSERT_EQUAL(SET, led_status_toggel(current));
}

But, I get an error: led_status_toggel() has remaining non-returned values. Do I have to create a mock file for my functions or what's the source of this error? Ps: I'm using unity.h as an assertions library.


Solution

  • According to your test, it seams that function you are testing is led_status_toggel. If that is the case, you should not mock it. You should just remove will_return(led_status_toggel,SET);, since your led_status_toggel is probably something like this (You dind't share it so I don't know exactly):

    state_t led_status_toggel(state_t state)
    {
        if (state == CLEAR)
        {
            return SET;
        }       
        return CLEAR;
    }
    

    If your function under test is not led_status_toggel, but some other (that you didn't mentioned) which calls this led_status_toggel, then you can mock this function like this

    state_t __wrap_led_status_toggel(state_t state)
    {
        return (state_t)mock();
    }
    

    and use -Wl,--wrap=led_status_toggel in your build command. With --wrap linker flag, when you execute your test, the mock function __wrap_led_status_toggel will be called instead of the original led_status_toggel.