Imre Kis | a21712e | 2019-10-08 12:56:59 +0200 | [diff] [blame^] | 1 | CppUMock |
| 2 | ======== |
| 3 | |
| 4 | CppUMock is the built-in mocking system of CppUTest. This chapter describes the basic features of the system. For details please |
| 5 | refer the `official CppUMock manual`_. |
| 6 | |
| 7 | System functions |
| 8 | ---------------- |
| 9 | |
| 10 | Checking expectations |
| 11 | ^^^^^^^^^^^^^^^^^^^^^ |
| 12 | |
| 13 | After defining expected calls an invoking actual call the test should check if all the expected calls have happened. This can be |
| 14 | done by calling ``mock().checkExpectations()``. The common place to put this function call is the ``TEST_TEARDOWN`` function of |
| 15 | the test group. |
| 16 | |
| 17 | |
| 18 | Resetting mocking system |
| 19 | ^^^^^^^^^^^^^^^^^^^^^^^^ |
| 20 | |
| 21 | After 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 | |
| 26 | Namespaces |
| 27 | ^^^^^^^^^^ |
| 28 | |
| 29 | All interactions with the mocking system start by calling ``mock()``. This function has an option ``name`` string parameter |
| 30 | which 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 | |
| 37 | Enable/disable |
| 38 | ^^^^^^^^^^^^^^ |
| 39 | |
| 40 | The mocking system can be enabled/disabled runtime using the functions below. This causes call expected and actual call to be |
| 41 | ignored. 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 | |
| 61 | String order |
| 62 | ^^^^^^^^^^^^ |
| 63 | |
| 64 | After defining multiple expected function call the mocking system always The mocking system always uses the next matching |
| 65 | function when an actual call happens but by default it doesn't check if different function calls happen in the order of |
| 66 | definition. This behaviour can be turned on using the ``mock().strictOrder()`` function. This option is also set to default by |
| 67 | the ``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 | |
| 86 | Ignoring unspecified calls |
| 87 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 88 | |
| 89 | If there are addition actual calls happening in the test case which are unrelated to the test case (i.e. log or other messages) |
| 90 | the unspecified calls can be ignored by adding ``mock().ignoreOtherCalls()`` to the test case. This function should be used |
| 91 | carefully 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 | |
| 95 | Specifying object |
| 96 | ----------------- |
| 97 | |
| 98 | In object oriented environment member function should validate if the function call happened on the suitable object. This is |
| 99 | done by adding the following function to the mock specification. |
| 100 | |
| 101 | - ``onObject(objectPtr)`` |
| 102 | |
| 103 | |
| 104 | Validating parameters |
| 105 | --------------------- |
| 106 | |
| 107 | Each supported parameter type has a corresponding function. These are the same |
| 108 | in 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 | |
| 122 | If custum types are defined and copier/comparator objects were installed the following function can handle these parameters. |
| 123 | |
| 124 | - ``withParameterOfType(typeName, name, value)`` |
| 125 | |
| 126 | There's an option to copying data from the test environment into the mock function. When setting expectations the following |
| 127 | function can be used to set the pointer and the address of the data. **The mocking system will not create a copy of this data** |
| 128 | so the original data should be kept intact until the actual call happens. |
| 129 | |
| 130 | - ``withOutputParameterReturning(name, value, size)`` |
| 131 | - ``withOutputParameterOfTypeReturning(typeName, name, value)`` |
| 132 | |
| 133 | In the actual call the pair of these function are shown below. |
| 134 | |
| 135 | - ``withOutputParameter(name, output)`` |
| 136 | - ``withOutputParameterOfType(typeName, name, output)`` |
| 137 | |
| 138 | |
| 139 | Ignoring parameters |
| 140 | ^^^^^^^^^^^^^^^^^^^ |
| 141 | |
| 142 | There are cases when the developer doesn't want to specify all parameters. The following function can set this behaviour in the |
| 143 | expected call. |
| 144 | |
| 145 | - ``ignoreOtherParameters()`` |
| 146 | |
| 147 | |
| 148 | Specifying return values |
| 149 | ------------------------ |
| 150 | |
| 151 | Using function name overloading the return values are specified by calling ``andReturnValue`` and the parameter type will |
| 152 | determine 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 | |
| 166 | Returning value in actual calls |
| 167 | ------------------------------- |
| 168 | |
| 169 | All of these function have version with ``OrDefault(type default_value)`` suffix. These version return a default value if the |
| 170 | return 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 | |
| 184 | Debugging CppUMock errors |
| 185 | ------------------------- |
| 186 | |
| 187 | Debugging CppUMock errors can be hard unlike assertion errors because a mocking failure can happen multiple layers of function |
| 188 | calls 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 |
| 190 | it 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 |