HippoMocks insists on destroying mock

1.1k views Asked by At
IEmployeeServiceProxy* empSvcMock = m_Mocks.InterfaceMock<IEmployeeServiceProxy>();
m_EmpSvcMock.reset(empSvcMock); // shared_ptr because my class Client ctor expects a shared_ptr<IEmployeeServiceProxy>

Client client(m_EmpSvcMock);

how to prevent m_EmpSvcMock from being destroyed internally by HippoMock? When passing a mock to a shared_ptr, both would destroy the mock.

EDIT - Answer:

m_Mocks.ExpectCallDestructor(m_EmpSvcMock.get());
m_EmpSvcMock.reset();
2

There are 2 answers

3
dascandy On BEST ANSWER

In the Git version (from Assembla) you can tell it to register a destructor to be called. Added bonus is that it will warn you about functions called on it after that with a ZombieMockException, so if you do leak a pointer somewhere and it gets used you'll know with a readable error.

1
stijn On

use a helper like this, which creates a shared_ptr with a no-op deleter:

template< class T >
void NoDelete( T* )
{
}

template< class T >
std::shared_ptr< T > make_shared_ref( T* t )
{
  return std::shared_ptr< T >( t, NoDelete< T > );
}

  //usage
m_EmpSvcMoc = make_shared_ref( empSvcMock );