cmocka
1.1.6
Unit testing library with mock support
|
Functions | |
LargestIntegralType | mock (void) |
Retrieve a return value of the current function. | |
type | mock_ptr_type (#type) |
Retrieve a typed return value of the current function. | |
void | will_return (#function, LargestIntegralType value) |
Store a value to be returned by mock() later. | |
void | will_return_count (#function, LargestIntegralType value, int count) |
Store a value to be returned by mock() later. | |
void | will_return_always (#function, LargestIntegralType value) |
Store a value that will be always returned by mock(). | |
void | will_return_maybe (#function, LargestIntegralType value) |
Store a value that may be always returned by mock(). | |
Mock objects are simulated objects that mimic the behavior of real objects. Instead of calling the real objects, the tested object calls a mock object that merely asserts that the correct methods were called, with the expected parameters, in the correct order.
will_return(function, value) - The will_return() macro pushes a value onto a stack of mock values. This macro is intended to be used by the unit test itself, while programming the behaviour of the mocked object.
Because the will_return() and mock() are intended to be used in pairs, the cmocka library would fail the test if there are more values pushed onto the stack using will_return() than consumed with mock() and vice-versa.
The following unit test stub illustrates how would a unit test instruct the mock object to return a particular value:
Now the mock object can check if the parameter it received is the parameter which is expected by the test driver. This can be done the following way:
For a complete example please take a look here.
LargestIntegralType mock | ( | void | ) |
Retrieve a return value of the current function.
type mock_ptr_type | ( | # | type | ) |
Retrieve a typed return value of the current function.
The value would be casted to type internally to avoid having the caller to do the cast manually.
[in] | type | The expected type of the return value |
Retrieve a typed return value of the current function.
The value would be casted to type internally to avoid having the caller to do the cast manually but also casted to uintptr_t to make sure the result has a valid size to be used as a pointer.
[in] | type | The expected type of the return value |
void will_return | ( | # | function, |
LargestIntegralType | value | ||
) |
Store a value to be returned by mock() later.
[in] | function | The function which should return the given value. | |
[in] | value | The value to be returned by mock(). |
void will_return_always | ( | # | function, |
LargestIntegralType | value | ||
) |
Store a value that will be always returned by mock().
[in] | function | The function which should return the given value. | |
[in] | value | The value to be returned by mock(). |
This is equivalent to:
void will_return_count | ( | # | function, |
LargestIntegralType | value, | ||
int | count | ||
) |
Store a value to be returned by mock() later.
[in] | function | The function which should return the given value. | |
[in] | value | The value to be returned by mock(). | |
[in] | count | The parameter indicates the number of times the value should be returned by mock(). If count is set to -1, the value will always be returned but must be returned at least once. If count is set to -2, the value will always be returned by mock(), but is not required to be returned. |
void will_return_maybe | ( | # | function, |
LargestIntegralType | value | ||
) |
Store a value that may be always returned by mock().
This stores a value which will always be returned by mock() but is not required to be returned by at least one call to mock(). Therefore, in contrast to will_return_always() which causes a test failure if it is not returned at least once, will_return_maybe() will never cause a test to fail if its value is not returned.
[in] | function | The function which should return the given value. | |
[in] | value | The value to be returned by mock(). |
This is equivalent to: