Skip to content

Commit ea8aa41

Browse files
author
Arnd R. Strube
committed
Add section on withOutputParameterOfTypeReturning
1 parent 6878cdf commit ea8aa41

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

mocking_manual.markdown

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The main idea is to make manual mocking easier, rather than to make automated mo
2121
* [Using Parameters](#parameters)
2222
* [Objects as Parameters](#objects_as_parameters)
2323
* [Output Parameters](#output_parameters)
24+
* [Output Parameters Using Objects](#output_parameters_using_objects)
2425
* [Return Values](#return_values)
2526
* [Passing other data](#other_data)
2627
* [Other MockSupport](#other_mock_support)
@@ -214,10 +215,10 @@ public:
214215

215216
The isEqual is called to compare the two parameters. The valueToString is called when an error message is printed and it needs to print the actual and expected values. If you want to use normal C functions, you can use the MockFunctionComparator which accepts pointers to functions in the constructor.
216217

217-
To remove the comparators, all you needs to do is call removeAllComparators, like:
218+
To remove the comparators, all you needs to do is call removeAllComparatorsAndCopiers, like:
218219

219220
{% highlight c++ %}
220-
mock().removeAllComparators();
221+
mock().removeAllComparatorsAndCopiers();
221222
{% endhighlight %}
222223

223224
Comparators sometimes lead to surprises, so a couple of warnings on its usage:
@@ -288,6 +289,54 @@ mock().expectOneCall("Foo").withOutputParameterReturning("bar", &doubleOutputVal
288289

289290
* When a 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.
290291

292+
<a id="output_parameters_using_objects"> </a>
293+
294+
### Output Parameters Using Objects
295+
296+
By far the best way to handle output parameters is by using a custom type copier (3.8). The general principle is similar to the custom comparators described above:
297+
298+
{% highlight c++ %}
299+
MyType outputValue = 4;
300+
mock().expectOneCall("Foo").withOutputParameterOfTypeReturning("MyType", "bar", &outputValue);
301+
{% endhighlight %}
302+
303+
The corresponding actual call is:
304+
305+
{% highlight c++ %}
306+
void Foo(int *bar)
307+
{
308+
mock().actualCall("Foo").withOutputParameterOfType("MyType", "bar", bar);
309+
}
310+
{% endhighlight %}
311+
312+
When using withOutputParameterOfTypeReturning, the mocking framework needs to know how to copy the type and therefore a Copier has to be installed before using parameters of this type. This is done using installCopier, as below:
313+
314+
{% highlight c++ %}
315+
MyTypeCopier copier;
316+
mock().installCopier("myType", copier);
317+
{% endhighlight %}
318+
319+
MyTypeCopier is a custom copier, which implements the MockNamedValueCopier interface. For example:
320+
321+
{% highlight c++ %}
322+
class MyTypeCopier : public MockNamedValueCopier
323+
{
324+
public:
325+
virtual void copy(void* out, const void* in)
326+
{
327+
*(MyType*)out = *(const MyType*)in;
328+
}
329+
};
330+
{% endhighlight %}
331+
332+
To remove the copier, you need to call removeAllComparatorsAndCopiers, like:
333+
334+
{% highlight c++ %}
335+
mock().removeAllComparatorsAndCopiers();
336+
{% endhighlight %}
337+
338+
*Warning 1* and *Warning 2* above apply to copiers as well.
339+
291340
<a id="return_values"> </a>
292341

293342
### Return Values

0 commit comments

Comments
 (0)