本文介绍了如何在google mock测试中将一个值设置为一个mock方法的void *参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想将字符串Device Name传递给方法的 void * 指针参数,并稍后将其检索到字符数组。 $ b 这里我创建了一个动作来实现这一点。 ACTION_P(SetArg2ToChar,value){* static_cast< char *>(arg2)= * value; } 被调用/模拟的实际方法 bool getDictItem(WORD wIndex,BYTE bSubIndex,void * pObjData,DWORD dwLength,CSdo& sdo) 我的mock方法 MOCK_METHOD5(getDictItem, bool(WORD wIndex, BYTE bSubIndex,void * pObjData,DWORD dwLength,CSdo& sdo)); 在代码中调用 if(!can.getDictItem(wIndex,bSubIndex,pObjData,dwLength,tSdo)) b $ b 我想向这个 pObjData (列表中的第三个参数)传递一个字符串。 在我的google测试中,我这样做。 char szDeviceName [30] = {0} snprintf(szDeviceName,sizeof(szDeviceName),%s,Device Name); EXPECT_CALL(mockCan,getDictItem(_,_,_,_,_)) .WillOnce(DoAll(SetArg2ToChar(szDeviceName), Return(true))) .RetiresOnSaturation(); / *调用一个真正的方法来调用这个mock方法* / 如果我尝试直接使用SetArgPointee 设置此参数(pObjData),我得到以下错误。因此,我尝试使用 ACTION_P 现在有了这个实现,我只得到 szDeviceName 变量(到这个 pObjData ),即,D,后面是在这个mock对象后的实际代码流中的29 0叫做。 我想获得设置为 void * 的完整字符串名称。 我指的是这个问题,并且能够取得进展。但我不能传递完整的字符串。 如何设置,在google mock中,一组值的void *参数? 有关这方面的任何信息都是有帮助的。 ACTION_P 方法基本上是正常的。但是当你处理一个C字符串时,你不能使用赋值操作(只是复制第一个字符),而应该使用一个字符串复制函数,如 ACTION_P(SetArg2ToCharWithSizeArg3,value){ strcpy_s(static_cast< char *>(arg2),arg3,value); } (我无法抗拒轻微重命名操作)。 I want to pass a string "Device Name" to a void * pointer argument of a method and retrieve it to a character array later.For this I've done as shown below.Here I have created an action to achieve this.ACTION_P(SetArg2ToChar, value) {*static_cast<char*>(arg2) = *value; }Actual method to be called/mockedbool getDictItem(WORD wIndex, BYTE bSubIndex, void * pObjData, DWORD dwLength, CSdo& sdo)My mock method MOCK_METHOD5(getDictItem, bool(WORD wIndex, BYTE bSubIndex, void * pObjData, DWORD dwLength, CSdo& sdo));in code it is called as if( !can.getDictItem(wIndex, bSubIndex, pObjData, dwLength, tSdo) )I want to pass a string to this pObjData (3rd argument in the list).In my google tests, I'm doing like this.char szDeviceName[30]= {0};snprintf(szDeviceName, sizeof(szDeviceName), "%s", "Device Name" );EXPECT_CALL( mockCan, getDictItem(_,_,_,_,_) ) .WillOnce(DoAll(SetArg2ToChar(szDeviceName), Return(true))) .RetiresOnSaturation();/* Call a real method within which this mock method is called */If I try to set this argument(pObjData) using "SetArgPointee<2>" directly, I get the below error.Hence I'm trying with ACTION_PNow with this implementation, I only get the first letter of the szDeviceName variable (into this pObjData) i.e., "D" followed by 29 0's in the real code flow after this mock object is called. I want to get the full string name set into this void * arguement. I refered to this below question and was able progress this far. But I'm not able to pass the full string. How to set, in google mock, a void* argument to a set of values?Any information regarding this will be helpful. 解决方案 The ACTION_P approach is basically OK. But as you are dealing with a C string, you can't just use the assignment operation (which just copies the first character) but instead you should use a string copy function like ACTION_P(SetArg2ToCharWithSizeArg3, value) { strcpy_s(static_cast<char*>(arg2), arg3, value); } (I couldn't resist to slightly rename the action). 这篇关于如何在google mock测试中将一个值设置为一个mock方法的void *参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-24 04:56