Question:
How to mock the function with output parameter with cppumock?
My solution is through one global parameter to change the output parameter in mock function.
But apparently this is not elegant code.
Could someone kiindly tell me that is there any other solution more elegant?
My question on Google groups:
1.
Bas Vodde's Answer:
Right now, the best way is probably to use SetData and GetData.
There has been some work on output parameters on a branch, but we’re not really happy with that at the moment :(
Bas的解释很简单,我在这里简单解释一下Bas的意思:
例如含有出参的函数为:
int mockFunction(int* outputParam)
{
*outputParam = mock().getData("
outputParam
").getIntValue());
return mock().actualCall(“ mockFunction”).returnValue(). getIntValue();
}
在TEST函数里面:
{
//omit
int* outputParam;
mock().setData("
outputParam
", 10);
mockFunction(
outputParam
);
//then *
outputParam should be 10.
}
虽然有一点和正常的语法规则有一点出入,但是目前也只能这样使用。
此外,针对一般的
mock().getData()函数由很多返回类型的选择:
mock().setData("data", 1);
mock().getData("data").getIntValue();
mock().setData("data", "string");
mock().getData("data").getStringValue();
mock().setData("data", 1.0);
mock().getData("data").getDoubleValue(), 0.05);
void * ptr = (void*) 0x001;
mock().setData("data", ptr);
mock().getData("data").getPointerValue());
void * ptr = (void*) 0x001;
mock().setDataObject("data", "type", ptr);
POINTERS_EQUAL(ptr, mock().getData("data").getObjectPointer());
STRCMP_EQUAL("type", mock().getData("data").getType().asCharString());
2.
Terry Yin's Answer:
One ‘solution’ I used is to treat function that has both return value and output parameters as multiple function calls:
int mockFunction(int* outputParam)
{
*outputParam = mock().actualCall(“mockFunction.outputParam”). returnValue().getIntValue();
return mock().actualCall(“ mockFunction”).returnValue(). getIntValue();
}
Terry的想法不知道大家有没有理解,呵呵。
如果你对Cpputest比较熟悉的话,应该清楚只需要在对应
mockFunction被调用的UT函数中同样设mock().
expectOneCalls
即可:
mock().
expectOneCall
(“
mockFunction.outputParam
”).
andReturnValue(10)
I like Terry's answer.