SaveArg + protobuf question

545 views
Skip to first unread message

Michael Jeung

unread,
Nov 5, 2009, 2:15:09 PM11/5/09
to Google C++ Mocking Framework
Hi guys,

I'd like to use the SaveArg action to inspect the parameter of one of
my mock function calls -- to make sure that my object under test is
passing the correct paraemeter -- but I'm a little confused about how
it's supposed to work.

Here's the original function prototype that I'm mocking:

virtual void HandleData( const google::protobuf::Message &payload );

Inside my test:

{
my_protobuf::Foo * data;

EXPECT_CALL(mock_object, HandleData( _ ))
.WillOnce(testing::SaveArg<0>(data));

object_under_test.doSomething(); // this function should call
HandleData()

// (Make asserts about data object here.)
}

This is not compiling. I suspect that I'm getting my pointers /
addresses mixed up. I've tried all the combinations I can think of,
but I haven't been able to make any headway. Any hints would be much
appreciated.

Thanks!
Michael

Vlad Losev

unread,
Nov 5, 2009, 3:39:43 PM11/5/09
to Michael Jeung, Google C++ Mocking Framework
Hi Michael,

I believe this isn't working because SaveArg uses the assignment operator to copy the value and protobufs do not support assignment operators. You can define a custom action as a workaround:

// ACTION Needs to be defined at namespace level
ACTION_P(SaveProtobuf, proto) { proto->CopyFrom(arg0); }
my_protobuf::Foo data;
EXPECT_CALL(mock_object, HandleData(_)).WilOnce(SaveProtobuf(&data);

Thanks!
Michael

HTH,
Vlad

Michael Jeung

unread,
Nov 6, 2009, 1:03:29 PM11/6/09
to Vlad Losev, Google C++ Mocking Framework
> I believe this isn't working because SaveArg uses the assignment operator to
> copy the value and protobufs do not support assignment operators. You can
> define a custom action as a workaround:
>
> // ACTION Needs to be defined at namespace level
> ACTION_P(SaveProtobuf, proto) { proto->CopyFrom(arg0); }
> my_protobuf::Foo data;
> EXPECT_CALL(mock_object, HandleData(_)).WilOnce(SaveProtobuf(&data);
>
> HTH,
> Vlad
>

Thanks for your help Vlad. That worked perfectly.

I have to say, I'm very impressed with how flexible googlemock is. It
seems like there's always a way to get at the data I'm interested in.
I'll have to do some more reading on how parameterized actions work.

Thanks!
-Michael

Reply all
Reply to author
Forward
0 new messages