blob: fd82d95f2cd58308208c95b1bfb7c4470c68d4df [file] [log] [blame]
Imre Kisa21712e2019-10-08 12:56:59 +02001CppUMock
2========
3
4CppUMock is the built-in mocking system of CppUTest. This chapter describes the basic features of the system. For details please
5refer the `official CppUMock manual`_.
6
7System functions
8----------------
9
10Checking expectations
11^^^^^^^^^^^^^^^^^^^^^
12
13After defining expected calls an invoking actual call the test should check if all the expected calls have happened. This can be
14done by calling ``mock().checkExpectations()``. The common place to put this function call is the ``TEST_TEARDOWN`` function of
15the test group.
16
17
18Resetting mocking system
19^^^^^^^^^^^^^^^^^^^^^^^^
20
21After the last interaction with the mocking system the developer should reset the state of the system by calling
22``mock().clear()``. The common place to put this function call is the ``TEST_TEARDOWN`` function of the test group after calling
23``mock().checkExpectations()``.
24
25
26Namespaces
27^^^^^^^^^^
28
29All interactions with the mocking system start by calling ``mock()``. This function has an option ``name`` string parameter
30which can be used for limiting the scope of the mocking operation.
31
32.. code-block:: C++
33
34 mock("bl1").expectOneCall("bl1_main");
35
36
37Enable/disable
38^^^^^^^^^^^^^^
39
40The mocking system can be enabled/disabled runtime using the functions below. This causes call expected and actual call to be
41ignored. Default settings are restored by ``mock().clear()``.
42
43- ``enable()``
44- ``disable()``
45
46.. code-block:: C++
47
48 mock().disable();
49 // All CppUMock calls are ignored after this point
50 mock().enable();
51 // CppUMock calls are working again
52
53 mock().disable();
54 // All CppUMock calls are ignored after this point
55 [...]
56
57 mock().clear();
58 // Default settings are restored
59
60
61String order
62^^^^^^^^^^^^
63
64After defining multiple expected function call the mocking system always The mocking system always uses the next matching
65function when an actual call happens but by default it doesn't check if different function calls happen in the order of
66definition. This behaviour can be turned on using the ``mock().strictOrder()`` function. This option is also set to default by
67the ``mock().clear()`` function.
68
69.. code-block:: C++
70
71 mock().expectOneCall("A");
72 mock().expectOneCall("B");
73
74 mock().actualCall("B");
75 mock().actualCall("A");
76 // No error
77
78 mock().strictOrder();
79 mock().expectOneCall("A");
80 mock().expectOneCall("B");
81
82 mock().actualCall("B"); // Error generated here
83 mock().actualCall("A");
84
85
86Ignoring unspecified calls
87^^^^^^^^^^^^^^^^^^^^^^^^^^
88
89If there are addition actual calls happening in the test case which are unrelated to the test case (i.e. log or other messages)
90the unspecified calls can be ignored by adding ``mock().ignoreOtherCalls()`` to the test case. This function should be used
91carefully because it can hide unexpected call which are indicating errors in the code. The affect of this call ends by calling
92``mock().clear()``.
93
94
95Specifying object
96-----------------
97
98In object oriented environment member function should validate if the function call happened on the suitable object. This is
99done by adding the following function to the mock specification.
100
101- ``onObject(objectPtr)``
102
103
104Validating parameters
105---------------------
106
107Each supported parameter type has a corresponding function. These are the same
108in the expected and actual calls.
109
110- ``withBoolParameter(name, bool value)``
111- ``withIntParameter(name, int value)``
112- ``withUnsignedIntParameter(name, unsigned int value)``
113- ``withLongIntParameter(name, long int value)``
114- ``withUnsignedLongIntParameter(name, unsigned long int value)``
115- ``withDoubleParameter(name, double value)``
116- ``withStringParameter(name, const char* value)``
117- ``withPointerParameter(name, void* value)``
118- ``withFunctionPointerParameter(name, void (*value)())``
119- ``withConstPointerParameter(name, const void* value)``
120- ``withMemoryBufferParameter(name, const unsigned char* value, size_t size)``
121
122If custum types are defined and copier/comparator objects were installed the following function can handle these parameters.
123
124- ``withParameterOfType(typeName, name, value)``
125
126There's an option to copying data from the test environment into the mock function. When setting expectations the following
127function can be used to set the pointer and the address of the data. **The mocking system will not create a copy of this data**
128so the original data should be kept intact until the actual call happens.
129
130- ``withOutputParameterReturning(name, value, size)``
131- ``withOutputParameterOfTypeReturning(typeName, name, value)``
132
133In the actual call the pair of these function are shown below.
134
135- ``withOutputParameter(name, output)``
136- ``withOutputParameterOfType(typeName, name, output)``
137
138
139Ignoring parameters
140^^^^^^^^^^^^^^^^^^^
141
142There are cases when the developer doesn't want to specify all parameters. The following function can set this behaviour in the
143expected call.
144
145- ``ignoreOtherParameters()``
146
147
148Specifying return values
149------------------------
150
151Using function name overloading the return values are specified by calling ``andReturnValue`` and the parameter type will
152determine the exact function.
153
154- ``andReturnValue(bool value)``
155- ``andReturnValue(int value)``
156- ``andReturnValue(unsigned int value)``
157- ``andReturnValue(long int value)``
158- ``andReturnValue(unsigned long int value)``
159- ``andReturnValue(double value)``
160- ``andReturnValue(const char* value)``
161- ``andReturnValue(void* value)``
162- ``andReturnValue(const void* value)``
163- ``andReturnValue(void (*value)())``
164
165
166Returning value in actual calls
167-------------------------------
168
169All of these function have version with ``OrDefault(type default_value)`` suffix. These version return a default value if the
170return value was not specified in the expected call.
171
172- ``bool returnBoolValue()``
173- ``int returnIntValue()``
174- ``unsigned int returnUnsignedIntValue()``
175- ``long int returnLongIntValue()``
176- ``unsigned long int returnUnsignedLongIntValue()``
177- ``double returnDoubleValue()``
178- ``const char * returnStringValue()``
179- ``void * returnPointerValue()``
180- ``const void * returnConstPointerValue()``
181- ``void (*returnFunctionPointerValue())()``
182
183
184Debugging CppUMock errors
185-------------------------
186
187Debugging CppUMock errors can be hard unlike assertion errors because a mocking failure can happen multiple layers of function
188calls under the test case. The mocking system has a very similar feature to CppUTest's ``UT_CRASH()`` which is
189``mock().crashOnFailure()``. By enabling this feature the code will crash on mocking errors and the developer could easily catch
190it with the debugger.
191
192
193--------------
194
195*Copyright (c) 2019-2021, Arm Limited. All rights reserved.*
196
197.. _`official CppUMock manual`: https://cpputest.github.io/mocking_manual.html