How to set a value to void * argument of a mock method in google mock testing?

≡放荡痞女 提交于 2020-01-13 11:04:18

问题


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/mocked

bool 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.

error: 'void' is not a pointer-to-object type*

Hence I'm trying with ACTION_P

Now 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.


回答1:


Rather then doing that, you could invoke a function (or a method) and copy the parameter.

Something like this in the source file where the test is :

int invokedPObjData;

bool FakeGetDictItem(WORD wIndex, BYTE bSubIndex, void * pObjData, DWORD dwLength, CSdo& sdo)
{
  // copy data. here I assumed it is an int
  invokedPObjData = *static_cast< int* >( pObjData );

  return true; // or whatever makes sense
}

in test :

EXPECT_CALL( mockCan, getDictItem(_,_,_,_,_) )
            .WillOnce(Call(FakeGetDictItem))
            .RetiresOnSaturation();

then later in test check what needs to be checked.




回答2:


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).




回答3:


I recently had a similar need and came up with this as a generic solution. It's based on the built-in SetArgPointee and has the same syntax:

template <size_t N, typename A>
class SetArgumentPointeeVoidAction {
public:
    explicit SetArgumentPointeeVoidAction(const A& value) : value_(value) {}
    void operator=(SetArgumentPointeeVoidAction const&) = delete;

    template <typename Result, typename ArgumentTuple>
    void Perform(const ArgumentTuple& args) const
    {
        ::testing::StaticAssertTypeEq<void, Result>();
        ::testing::StaticAssertTypeEq<void*,
            std::decay<decltype(::testing::get<N>(args))>::type>();
        *static_cast<A*>(::testing::get<N>(args)) = value_;
    }

private:
    const A value_;
};

/**
 * \brief  Sets a \c void* output argument to the contents of the
 *         supplied object.  It's on you to ensure this is safe.
 * \tparam N The argument index.
 * \tparam T The real argument type.
 * \param  x The argument to assign to the output argument.
 * \return A GMock Action that performs the requested assignment.
 * \note   Use \c SetArgPointee when it's not a \c void*.
 */
template <size_t N, typename T>
::testing::PolymorphicAction< SetArgumentPointeeVoidAction<N, T> >
SetArgPointeeVoid(const T& x)
{
    return ::testing::MakePolymorphicAction(
        SetArgumentPointeeVoidAction<N, T>(x));
}

It will give you a compile error if you try to use this on an argument that isn't a void*, so it should be relatively safe as long as you ensure you supply the correct argument.

It's also possible to implement this using ACTION_TEMPLATE, which is a bit shorter, but it generates unused argument warnings, which can be irritating.

(In older versions of GMock you might have to use ::std::tr1::get instead of ::testing::get.)

Left as an exercise for the reader: it's possible to enhance this with perfect forwarding to allow this to move-construct and move-assign for a slight efficiency boost. Although if you're passing anything other than PODs around as void*s then you're probably doing it wrong.




回答4:


Here is an example using ACTION_TEMPLATE allowing a string to be assigned to a void *, for reference...

ACTION_TEMPLATE(StrCpyArgToVoidPointer,
                HAS_1_TEMPLATE_PARAMS(int, k),
                AND_2_VALUE_PARAMS(value, size))
{
    strncpy(static_cast<char *>(::testing::get<k>(args)), value, size);
    return;
}


来源:https://stackoverflow.com/questions/25788594/how-to-set-a-value-to-void-argument-of-a-mock-method-in-google-mock-testing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!