ThrowTheSwitch / CMock

CMock - Mock/stub generator for C
http://throwtheswitch.org
MIT License
652 stars 269 forks source link

Question: How to call mock function if function in test calls them multiple times with different expectations inside #443

Closed pathakraul closed 3 months ago

pathakraul commented 1 year ago

Hi, I am unable to find any way to test this scenario.

I have mocked version of read and write and a function func_to_test() which calls them multiple times.

void func_to_test()
{
    ret = read(addr1, &val1);
    ret = write(addr1, val1);

    ret = read(addr2, &val2);
    ret = write(addr2, &val2);

    .....
}

TEST()
{
         read_****();
         write_****();
         TEST_ASSERT_***(..., func_to_test())
}

In this case how can I call the mocked versions read_***() and write_***() before the TEST_ASSERT_** which calls my func_to_test such that they take affect with different values for different calls inside the test function. Or what is the right way to test this case

Thanks, Rahul

mvandervoord commented 3 months ago

In your test, you may set up as many expectations as desired, in order... so in your instance, it would look like this:

TEST()
{
         read_ExpectAndReturn();
         write_ExpectAndReturn();
         read_ExpectAndReturn();
         write_ExpectAndReturn();
         TEST_ASSERT_***(..., func_to_test())
}

I suspect you'd also want to look into the return-thru-pointer plugin to handle the val1, and val2 returns in the read functions.