Gmock returning a mock by value

632 views
Skip to first unread message

Max Davies-Smith

unread,
Feb 22, 2016, 10:34:05 AM2/22/16
to googl...@googlegroups.com

Hi.

I think the answer is 'no dont be silly' but it's worth asking.
I have a function the returns objects by value. Internally it uses std::move on the return but probably an irrelevant point since elision is there to help.
What this function returns is a type I have mocked. On which I have set expectations.
Firstly, it don't compile
Secondly if it did the expectations wouldn't work because it would be a copy - i guess.
It's annoying to have to return a smart ptr when I really don't want the heap cost and the scope is small.
So is there a gmock way to do what is typical and optimal ?
Regards
Max

Corey Kosak

unread,
Feb 24, 2016, 7:18:42 PM2/24/16
to Max Davies-Smith, Google C++ Mocking Framework
I don't see how this could ever work.

The mock class is a subclass of the original class. If your return type is that of the original class, you're going to slice the object, just as you would in any other C++ inheritance scenario (i.e. this is a problem not limited to gMock).

It might--might--be possible to do this by using the techniques described here under "Mocking Nonvirtual Methods", but one thing I'm not sure about is whether--as you say--the expectations are copied when the object is copied.


--

---
You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googlemock+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/googlemock/CA%2Bry%2B0Khj2ruc4NThuoO7bBn8VR%3DNFdo6671b0LgV%2BpPUCvrqA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages