You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: mocking_manual.markdown
+11Lines changed: 11 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,6 +29,7 @@ The main idea is to make manual mocking easier, rather than to make automated mo
29
29
*[C Interface](#c_interface)
30
30
31
31
<aid="simple_scenario"> </a>
32
+
32
33
### Simple Scenario
33
34
34
35
About the simplest scenario is to check that one particular function call has happened. The below code is an example of this:
@@ -99,6 +100,7 @@ If the call to productionCode wouldn't happen, then the test would fail with the
99
100
<none>
100
101
101
102
<aid="objects"> </a>
103
+
102
104
### Using Objects
103
105
104
106
Simple scenario using objects
@@ -150,6 +152,7 @@ If the call to a wrong object happens, it would give the following error message
150
152
<none>
151
153
152
154
<aid="parameters"> </a>
155
+
153
156
### Parameters
154
157
155
158
Of course, just checked whether a function is called is not particularly useful when we cannot check the parameters. Recording parameters on a function is done like this:
@@ -175,6 +178,7 @@ If a parameter isn't passed, it will give the following error:
175
178
int p1, char* p2
176
179
177
180
<aid="objects_as_parameters"> </a>
181
+
178
182
### Objects as Parameters
179
183
180
184
withParameters can only use int, double, const char* or void* . However, parameters are often objects of other types and not of the basic types. How to handle objects as parameters? Below is an example:
@@ -230,6 +234,7 @@ Comparators are *not* copied, instead it uses the exact instance as passed to th
230
234
When using the MockPlugin (recommended), then its best to install the comparators via the MockPlugin or put them in global space. The checkExpectations will be called *after* teardown and if your comparator was destroyed in the teardown then this will cause a crash.
231
235
232
236
<aid="output_parameters"> </a>
237
+
233
238
### Output Parameters
234
239
235
240
Some parameters do not represent data passed to the called function, but are passed by reference so that the function can 'return' a value by modifying the pointed-to data.
* When an char, int, etc. array is passed to withOutputParameter, you must use the generic withOutputParameterReturning and provide the actual size of the array or only one element will be copied.
284
289
285
290
<aid="return_values"> </a>
291
+
286
292
### Return Values
287
293
288
294
Sometimes it is needed to let a mock function return a value which can then be used in production code. The test code would look like this:
@@ -311,6 +317,7 @@ int function () {
311
317
The return value options are used to transfer data between the test and the mock object, they themselves do not cause the tests to fail.
312
318
313
319
<aid="other_data"> </a>
320
+
314
321
### Passing other data
315
322
316
323
Sometimes a test wants to pass more data to the mock object to, for example, vary only a couple of parameters in a calculation. This can be done like this:
Like return values. Setting data will not ever make a test fail but it provides support in building mock objects.
333
340
334
341
<aid="other_mock_support"> </a>
342
+
335
343
### Other MockSupport - ignoring, enabling, clearing, crashing
336
344
337
345
MockSupport offers a couple of other useful functions, which will be covered in this section.
@@ -376,6 +384,7 @@ When using gdb, get a stack trace using:
376
384
(r is run, it will run until crashes. bt is back trace which will produce a stack)
377
385
378
386
<aid="mock_scope"> </a>
387
+
379
388
### MockSupport Scope
380
389
381
390
MockSupport can be used hierarchically using MockSupport scope. This sounds really complex, but in reality it is very simple. When getting a mock support using the mock function, you can pass a namespace or scope and record the expectations (or do other things) inside this scope. For example:
CppUTest plugins can be installed in the main and 'extent' the unit test framework. It is a place where you can put work that needs to be done in all unit tests. There is a MockPlugin to make the work with mocks easier. It does the following work:
This code creates a comparator for MyDummy and installs it at the plugin. This means the comparator is available for all test cases. It creates the plugin and installs it at the current test registry. After installing the plugin, you don't have to worry too much anymore about calling checkExpectations or cleaning your MockSupport.
0 commit comments