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

1

11

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.

Nodus answered 21/8, 2013 at 16:33 Comment(0)
H
18

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)));
Henrik answered 21/8, 2013 at 21:9 Comment(1)
I obviously didn't understand actions before now, heh. Thank you for the help!Nodus

© 2022 - 2024 — McMap. All rights reserved.