cmocka  1.1.6
Unit testing library with mock support
Loading...
Searching...
No Matches
Functions
Call Ordering
Collaboration diagram for Call Ordering:

Functions

void function_called (void)
 Check that current mocked function is being called in the expected order.
 
void expect_function_calls (#function, const int times)
 Store expected call(s) to a mock to be checked by function_called() later.
 
void expect_function_call (#function)
 Store expected single call to a mock to be checked by function_called() later.
 
void expect_function_call_any (#function)
 Expects function_called() from given mock at least once.
 
void ignore_function_calls (#function)
 Ignores function_called() invocations from given mock function.
 

Detailed Description

It is often beneficial to make sure that functions are called in an order. This is independent of mock returns and parameter checking as both of the aforementioned do not check the order in which they are called from different functions.

expect_function_call() and function_called() are intended to be used in pairs. Cmocka will fail a test if there are more or less expected calls created (e.g. expect_function_call()) than consumed with function_called(). There are provisions such as ignore_function_calls() which allow this restriction to be circumvented in tests where mock calls for the code under test are not the focus of the test. function_called() must be called from the same thread as expect_function_call(), and that thread must have been initialized for use by cmocka (see also the Threading section of the main documentation page).

The following example illustrates how a unit test instructs cmocka to expect a function_called() from a particular mock, chef_sing():

void chef_sing(void);
void code_under_test()
{
chef_sing();
}
void some_test(void **state)
{
code_under_test();
}
void expect_function_call(#function)
Store expected single call to a mock to be checked by function_called() later.

The implementation of the mock then must check whether it was meant to be called by invoking function_called():

void chef_sing()
{
}
void function_called(void)
Check that current mocked function is being called in the expected order.

Function Documentation

◆ expect_function_call()

void expect_function_call ( function)

Store expected single call to a mock to be checked by function_called() later.

Parameters
[in]functionThe function which should should be called
See also
function_called()

◆ expect_function_call_any()

void expect_function_call_any ( function)

Expects function_called() from given mock at least once.

Parameters
[in]functionThe function which should should be called
See also
function_called()

◆ expect_function_calls()

void expect_function_calls ( function,
const int  times 
)

Store expected call(s) to a mock to be checked by function_called() later.

Parameters
[in]functionThe function which should should be called
[in]timesnumber of times this mock must be called
See also
function_called()

◆ function_called()

void function_called ( void  )

Check that current mocked function is being called in the expected order.

See also
expect_function_call()

◆ ignore_function_calls()

void ignore_function_calls ( function)

Ignores function_called() invocations from given mock function.

Parameters
[in]functionThe function which should should be called
See also
function_called()