title: C++ Google mock/unit tests : Mock method not called and original one is
body: <p>I am mocking a method but it seems that my <code>GoogleTest</code> keeps calling the original implementation. Probably missing something but can't find out what is this?</p>
  
  <p>My class is a Postgres database accessor:</p>
  
  <pre><code>namespace project
  {
  
  class PostgresDb
  {
  public :
      // typedef the CursorPtr, this is just a smart pointer.
      // sth like 
      typedef shared_ptr&lt;DbDataType&gt; CursorPtr ;
  
      PostgresDb( const std::string &amp; connString ) ;
  
      CursorPtr getRecords( const int id_1 ,
                            const int id_2 ) ;
  
  protected :
  
      // this will be used by the MockPostgresDb in our unit tests.
      // it leaves the object uninitialized
       PostgresDb (){} ;
  
   }
  
  }// end of namespace
  </code></pre>
  
  <p>and here is my mock class:</p>
  
  <pre><code>namespace project_test
  {
  
  class MockPostgresDb : public project::PostgresDb
  {
  public :
      MockPostgresDb(){} ;
      MOCK_METHOD2( getRecords, CursorPtr*( const int , const int  ) ) ;
  } ;
  
  class MockCursor : public CursorPtr
  {
      // ....
  }
  
  }
  </code></pre>
  
  <p>and here is the method I am testing and the test:</p>
  
  <pre><code>void original_get_method( const int id_1, const int id_2 , PostgresDb db)
  {
      // ....
      db.getRecords( id_1, id_2 ) ;
      // ....
  }
  
  // helper function
  setupGetRecords( MockPostgresDb* db     ,
                   MockCursor*     cursor ) 
  {
      EXPECT_CALL( *db, getRecords(_,_) )
          .Times( 1 )
          .WillRepeatedly(::testing::Return( cursor ) ) ;
  }
  
  
  TEST_F(....)
  {
      MockPostgresDb db ;
      MockCursor     cursor ;
      // ....
      setupGetRecords( &amp;db, &amp;cursor ) ; 
      // ....
      // and then calling a functi
      original_method( id_1, id_2, db ) ;
  }
  </code></pre>
  
  <p>So in my mind, I am calling <code>original_method</code> and pass a <code>mock_db</code>. The <code>mock_db</code> calls its method <code>getRecords</code> which returns a <code>MockCursor</code>. This is where it should get me the mock one but I do enter the <code>db::getRecords</code>.</p>
  
  <p>I am trying to find where the mismatch is but cannot figure out.</p>
  
  <p>EDIT :</p>
  
  <p>So, as pointed out - <code>getRecords</code> should return a <code>CursorPtr</code> and not a <code>CursorPtr*</code>. So here is what I did:</p>
  
  <p>I also tried to change</p>
  
  <pre><code>MOCK_METHOD2( getRecords, CursorPtr*( const int , const int  ) ) ;`
  </code></pre>
  
  <p>to </p>
  
  <pre><code>MOCK_METHOD2( getRecords, CursorPtr( const int , const int  ) ) ;  // ( no * ) 
  </code></pre>
  
  <p>And change the helper to</p>
  
  <pre><code>// helper function
  setupGetRecords( MockPostgresDb* db     ,
                   MockCursor     cursor ) 
  {
      EXPECT_CALL( *db, getRecords(_,_) )
          .Times( 1 )
          .WillRepeatedly(::testing::Return( cursor ) ) ;
  }
  </code></pre>
  
  <p>and getting some mismatched types of compilation errors. What went wrong? Thanks.</p>