cmocka
1.0.1
|
Mock objects mock objects are simulated objects that mimic the behavior of real objects. More...
![]() |
Functions | |
LargestIntegralType | mock (void) |
Retrieve a return value of the current function. More... | |
type | mock_ptr_type (#type) |
Retrieve a typed return value of the current function. More... | |
void | will_return (#function, LargestIntegralType value) |
Store a value to be returned by mock() later. More... | |
void | will_return_always (#function, LargestIntegralType value) |
Store a value that will be always returned by mock(). More... | |
void | will_return_count (#function, LargestIntegralType value, int count) |
Store a value to be returned by mock() later. More... | |
Mock objects 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 at 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 |
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 returns the number of times the value should be returned by mock(). If count is set to -1 the value will always be returned. |