I have a function that expects a buffer length input in a pointer argument, and then puts the number of the read bytes into that same argument as output. Now in my unit test I try to mock it with Hippomocks, and would like to check if the function was called with the correct input value, and at the same time, provide a different output value. How could I achieve this? Thanks!
Hippomocks pointer argument - provide input, check output
244 views Asked by YaniMan At
2
There are 2 answers
0
On
You can do it for the function
void addingFunc(const int* inputArg_p, int* returnArg_p)
{
*returnArg_p = *inputArg_p +1;
}
with
const int expected = 42;
int readbackActual = 0;
int readbackActual_p = &readbackActual;
const int writeValue = 1;
const int* writeValue_p = writeValue;
mocks.ExpectFuncCall(someFunc).With(Out(writeValue_p), In(readbackActual_p ); //Writes writeValue into *inputArg_p, then writes *returnArg_p into *readbackActual_p.
funcUnderTest(); //Function which uses addingFunc somehow.
ASSERT("FooBarError", expected == readbackActual); //Will fail: 42 is unequal to 1+1 !
(These unpretty pointers are used because Hippomocks complains about types when using &readbackActual directly.)
Picking in my memory since I haven't used Hippomocks in a long time, I can't give the exact syntax, but that's what Hippomocks'
Do()
method is for.In Hippomocks' test suite you can find a usage example with a lambda expression: https://github.com/dascandy/hippomocks/blob/master/HippoMocksTest/test_do.cpp#L20.