Mocked class not overriding original class

2,296 views
Skip to first unread message

doug livesey

unread,
Apr 29, 2012, 1:04:43 PM4/29/12
to googl...@googlegroups.com
Hi -- I'm having a few issues getting google mock to work, so I created a POC test file in my current project, here: https://github.com/biot023/improc/blob/master/test/poc/poc_test.cpp
This, if I have things aright, should mock out the class Poc1, and test the class Poc2's utilisation of it.
To test this, I have the Poc1 class setup to return the string "poc1 string", and setup the mocked version to return "mock poc1 string".
Unfortunately, when I run this test, the string "poc1 string" is returned, and the expectation that MocPoc1::get_str should be called fails.
If anyone could have a look over the file (it's small and self-contained) & tell me where I'm going wrong, I'd be really, really grateful.
Thanks,
   Doug.

Vlad Losev

unread,
Apr 30, 2012, 7:10:41 PM4/30/12
to Google C++ Mocking Framework
Doug -

You are defining a const method in Poc1 and then overriding it with non-const method in MockPock1. In order to generate a const mock method, use MOCK_CONST_METHOD0. See http://code.google.com/p/googlemock/wiki/ForDummies#Writing_the_Mock_Class for more information of that.

HTH,
Vlad

Vlad Losev

unread,
May 1, 2012, 2:16:07 PM5/1/12
to Google C++ Mocking Framework
[replying to the list]

It's not the compiler. Looking closer, your object container (Poc2) holds the object by value and not by reference. When you pass it a mock in the constructor, it simply copies the string from the mock's base class and the mock is never used after that in your program. This is one of the reasons why it's not recommended to derive mocks from objects under test. Instead, have both the object under test and the mock implement the same interface and have your containers hold the interface pointer.

Regards,
Vlad

On Tue, May 1, 2012 at 3:14 AM, doug livesey <bio...@gmail.com> wrote:
Ah, thanks for that -- so I was!
Unfortunately, after fixing it, I still have the same error. I even tried removing the const from the method and using the non-const MOCK_* macro, but that didn't work.
Could it be the compiler I'm using? (GCC 4.7)
Reply all
Reply to author
Forward
0 new messages