OpenVPN
Macros
Mock Objects

Mock objects mock objects are simulated objects that mimic the behavior of real objects. More...

Collaboration diagram for Mock Objects:

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)
 

Detailed Description

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.

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:

int chef_cook(const char *order, char **dish_out)
{
}

For a complete example please at a look here.

Macro Definition Documentation

◆ mock

#define mock ( )    _mock(__func__, __FILE__, __LINE__)

◆ mock_ptr_type

#define mock_ptr_type (   type)    ((type) (uintptr_t) mock())

Definition at line 263 of file cmocka.h.

Referenced by __wrap_chef_cook(), and mock_query_database().

◆ mock_type

#define mock_type (   type)    ((type) mock())

Definition at line 236 of file cmocka.h.

Referenced by __wrap_chef_cook().

◆ will_return

#define will_return (   function,
  value 
)
Value:
_will_return(#function, __FILE__, __LINE__, \
#define cast_to_largest_integral_type(value)
Definition: cmocka.h:96
void _will_return(const char *const function_name, const char *const file, const int line, const LargestIntegralType value, const int count)
Definition: cmocka.c:943

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_server_key_file(), and test_will_return_fails_for_no_calls().

◆ will_return_always

#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().

◆ will_return_count

#define will_return_count (   function,
  value,
  count 
)
Value:
_will_return(#function, __FILE__, __LINE__, \
#define cast_to_largest_integral_type(value)
Definition: cmocka.h:96
void _will_return(const char *const function_name, const char *const file, const int line, const LargestIntegralType value, const int count)
Definition: cmocka.c:943

Definition at line 317 of file cmocka.h.

Referenced by test_will_return_count_fails_for_unreturned_items().

◆ will_return_maybe

#define will_return_maybe (   function,
  value 
)    will_return_count(function, (value), -2)