OpenVPN
|
Mock objects mock objects are simulated objects that mimic the behavior of real objects. More...
Macros | |
#define | mock() _mock(__func__, __FILE__, __LINE__) |
#define | mock_type(type) ((type) mock()) |
#define | mock_ptr_type(type) ((type) (uintptr_t) mock()) |
#define | will_return(function, value) |
#define | will_return_count(function, value, count) |
#define | will_return_always(function, value) will_return_count(function, (value), -1) |
#define | will_return_maybe(function, value) will_return_count(function, (value), -2) |
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.
#define mock | ( | ) | _mock(__func__, __FILE__, __LINE__) |
Definition at line 210 of file cmocka.h.
Referenced by __wrap_buffer_read_from_file(), __wrap_buffer_write_file(), binary_operator(), connect_to_database(), and mock_function().
Definition at line 263 of file cmocka.h.
Referenced by __wrap_chef_cook(), and mock_query_database().
#define mock_type | ( | type | ) | ((type) mock()) |
Definition at line 236 of file cmocka.h.
Referenced by __wrap_chef_cook().
#define will_return | ( | function, | |
value | |||
) |
Definition at line 294 of file cmocka.h.
Referenced by test_bad_dish(), test_connect_to_customer_database(), test_connect_to_product_database(), test_connect_to_product_database_bad_url(), test_connect_to_product_database_missing_parameter(), test_get_customer_id_by_name(), test_order_hotdog(), test_perform_operation(), test_tls_crypt_v2_write_client_key_file(), test_tls_crypt_v2_write_client_key_file_metadata(), test_tls_crypt_v2_write_server_key_file(), and test_will_return_fails_for_no_calls().
#define will_return_always | ( | function, | |
value | |||
) | will_return_count(function, (value), -1) |
Definition at line 340 of file cmocka.h.
Referenced by test_will_return_always_fails_for_no_calls().
#define will_return_count | ( | function, | |
value, | |||
count | |||
) |
Definition at line 317 of file cmocka.h.
Referenced by test_will_return_count_fails_for_unreturned_items().
#define will_return_maybe | ( | function, | |
value | |||
) | will_return_count(function, (value), -2) |
Definition at line 368 of file cmocka.h.
Referenced by test_will_return_maybe_for_more_than_one_call(), test_will_return_maybe_for_no_calls(), and test_will_return_maybe_for_one_mock_call().