How to expect a call on a mock object in a shared_ptr?

8,560 views
Skip to first unread message

doug livesey

unread,
May 1, 2012, 8:43:28 PM5/1/12
to googl...@googlegroups.com
Hi -- sorry to be spamming the group, I'm reading all the docs and previous threads, honest! :)
I'm running into this compilation error when I try to set an expectation of a call on a mock object in a shared_ptr:
  test/poc/poc_test.cpp: In constructor '{anonymous}::PocTest::PocTest()':
  test/poc/poc_test.cpp:95:1: error: 'const struct poc::IPoc1' has no member named 'gmock_get_str'


As you can see, I'm dereferencing the shared_ptr, which *should* work, shouldn't it?
I've tried using Pointee, too, which didn't work, nor did dereferencing the pointer obtained explicitly by shared:ptr<>::get()
Could anyone advise me on how to set the expectation I need?
& again, sorry for all the questions -- I'm getting there, I promise.
Cheers,
   Doug.

Vlad Losev

unread,
May 1, 2012, 9:12:55 PM5/1/12
to Google C++ Mocking Framework
You should pass the interface pointer into the collaborating objects, but EXPECT_CALL manipulates the state of the mock, so it expects to see the mock itself, not an interface pointer/reference.
 
Changing your code to

 struct PocTest : public Test
 {
  protected:
    string _msg;
    shared_ptr<const MockPoc1> _poc1;
    PartMockPoc2 _poc2;
    PocTest() :
      _msg( "mock poc1 string" ),
      _poc1( make_shared<const MockPoc1>() )
    {
      EXPECT_CALL( *_poc1, get_str() )
        .Times( 1 )
        .WillOnce( Return( _msg ) );
      EXPECT_CALL( _poc2, _get_poc1() )
        .Times( 1 )
        .WillOnce( Return( shared_ptr<const IPoc1>(_poc1) ) ); // explicit conversion required.
    }
  };

should work. But be be very careful when using shared pointers - they are prone to forming loops and leaking memory - and gmock stores its own copies of objects you pass into its actions and/or matchers.
Reply all
Reply to author
Forward
0 new messages