How to mock the function with output parameter by cppumock?

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值