Loading...
Searching...
No Matches
🔢 Call Ordering
📚 The CMocka API

Ensure functions are called in the correct sequence. More...

## 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

Ensure functions are called in the correct sequence.

Verify that functions are called in the expected order.

This module provides functionality to ensure functions are called in a specific sequence, independent of mock return values and parameter checking. Both of the aforementioned do not verify the order in which functions are called.

- expect_function_call(function) - The expect_function_call() macro pushes an expectation onto the stack of expected calls.

- function_called() - pops a value from the stack of expected calls. function_called() is invoked within the mock object that uses it.

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)
{
expect_function_call(chef_sing);
code_under_test();
}
expect_function_call
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()
{
function_called();
}
function_called
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] |  | function | The 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] |  | function | The 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] |  | function | The function which should should be called
  [in] |  | times | number 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] |  | function | The function which should should be called

See also
  function_called()