Re: [googletest] Making Copy of Mock Objects

1,070 views
Skip to first unread message

Vlad Losev

unread,
Oct 2, 2011, 3:15:49 AM10/2/11
to Google C++ Mocking Framework
[-googletes...@googlegroups.com +googl...@googlegroups.com]



On Fri, Sep 30, 2011 at 3:02 AM, Daniel Graupner <Da...@dael.de> wrote:
Hello

assume the following mocked method:
 MOCK_CONST_METHOD1(fun, void ( OtherMock& ret));

The return value is by reference and type is another mock object.
Assigning a value to ret always involves making a copy. Unfortunately,
I have no ide how to copy mock objects in the sense that all
expectation will be copied too.

How can I achieve this?

In short, you can't. The mock objects generated by Google Mock are uncopyable. Check out this discussion for the background info. You can copy a pointer or a reference to a mock object, though.

Regards
Daniel

Dael

unread,
Oct 4, 2011, 1:12:31 AM10/4/11
to Google C++ Mocking Framework
Hello
assume the following mocked method:
MOCK_CONST_METHOD1(fun, void ( OtherMock& ret));

The return value is by reference and type is another mock object.
Assigning a value to ret always involves making a copy.
Unfortunately,
I have no ide how to copy mock objects in the sense that all
expectation will be copied too.

How can I achieve this?

Regards
Daniel

Vlad Losev

unread,
Oct 4, 2011, 1:24:31 AM10/4/11
to da...@dael.de, Google C++ Mocking Framework
Oops, forgot to add you.

Keith Ray

unread,
Oct 4, 2011, 12:58:20 PM10/4/11
to Dael, Google C++ Mocking Framework
besides various positive reasons for making a mock object un-copyable (the framework doesn't know how deep you want your copies to be, expectations might not be safely copyable, and so on), Gmock works to mock a domain method by overriding a virtual function. The declaration you have as an example would not override a function that is declared using domain types, because the argument list is using a test-only (mock) type.

> MOCK_CONST_METHOD1(fun, void ( OtherDomainType& ret));

When declared this way, it becomes more obvious that assigning to "ret" will use the OtherDomainType's assignment operator. That assignment will invoke C++'s "slicing problem" (copying base class data, not subclass data). Unless you make operator= a virtual function, which still won't work because mock objects are explicity not copyable.

What you MIGHT be able to do to get around this is to use smart pointers.

> MOCK_CONST_METHOD1(fun, void ( shared_ptr<OtherDomainType>& ret));


But I expect making that work will involve rewriting your code a great deal. You would need a factory function in your domain code that can be overridden in your test code. The factory function creates OtherDomainType objects using "new" and your test version of the factory function creates OtherMock objects using "new".

C. Keith Ray
http://agilesolutionspace.blogspot.com/
twitter: @ckeithray

Dael

unread,
Oct 4, 2011, 1:07:01 PM10/4/11
to Google C++ Mocking Framework
Yes, I understand the issue. What I want are fake objects and the
cookbook describes this (http://code.google.com/p/googlemock/wiki/
V1_6_CookBook#Mocking_Nonvirtual_Methods). I can do this without
gmock, but it would help me a lot. Fake objects with gmock are
smarter, need less lines of code etc.

Daniel

On 4 Okt., 18:58, Keith Ray <keith....@gmail.com> wrote:
> besides various positive reasons for making a mock object un-copyable (the framework doesn't know how deep you want your copies to be, expectations might not be safely copyable, and so on), Gmock works to mock a domain method by overriding a virtual function. The declaration you have as an example would not override a function that is declared using domain types, because the argument list is using a test-only (mock) type.
>
> > MOCK_CONST_METHOD1(fun, void ( OtherDomainType& ret));
>
> When declared this way, it becomes more obvious that assigning to "ret" will use the OtherDomainType's assignment operator. That assignment will invoke C++'s "slicing problem" (copying base class data, not subclass data). Unless you make operator= a virtual function, which still won't work because mock objects are explicity not copyable.
>
> What you MIGHT be able to do to get around this is to use smart pointers.
>
> > MOCK_CONST_METHOD1(fun, void ( shared_ptr<OtherDomainType>& ret));
>
> But I expect making that work will involve rewriting your code a great deal. You would need a factory function in your domain code that can be overridden in your test code. The factory function creates OtherDomainType objects using "new" and your test version of the factory function creates OtherMock objects using "new".
>
> C. Keith Rayhttp://agilesolutionspace.blogspot.com/
> twitter: @ckeithray
Reply all
Reply to author
Forward
0 new messages