Why can't I use SetArgPointee() with googlemock?

ladenedge picture ladenedge · Aug 21, 2013 · Viewed 11.7k times · Source

I am trying to set an "out" parameter on a mock with SetArgPointee. Here's the code I'm testing:

DWORD bodysize = 1024;
char body[1024];
HRESULT hr = req->ReadEntityBody(body, bodysize, false, &bodysize, NULL);

req is an IHttpRequest*, and I want to set bodysize, argument 3, in the mock. Here's the code from my unit test:

EXPECT_CALL(req, ReadEntityBody(NotNull(), Ge(1024), false, NotNull(), _))
    .WillOnce(SetArgPointee<3>(4))
    .WillOnce(Return(ERROR_HANDLE_EOF));

This results in the following strange error:

error C2440: 'return' : cannot convert from 'void' to 'long' (gmock/gmock-actions.h:369)

If I comment out .WillOnce(SetArgPointee<3>(4)), the code compiles and executes correctly. Here is the relevant definition in the mock itself:

MOCK_METHOD5(ReadEntityBody, HRESULT(VOID *pvBuffer, DWORD cbBuffer, BOOL fAsync, DWORD *pcbBytesReceived, BOOL *pfCompletionPending));

And, in case it helps, my full mock of IHttpRequest.

Answer

RA. picture RA. · Aug 21, 2013

Your actions are not being combined properly. As such, your EXPECT_CALL declaration is lacking a Return, which is why you are getting the error about trying to convert void to long. Ensure that you are using a DoAll():

EXPECT_CALL(req, ReadEntityBody(NotNull(), Ge(1024), false, NotNull(), _))
    .WillOnce(DoAll(SetArgPointee<3>(4), Return(ERROR_HANDLE_EOF)));