3
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();
BЈовић
  • 62,405
  • 41
  • 173
  • 273
Zach Saw
  • 4,308
  • 3
  • 33
  • 49

2 Answers2

2

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 );
stijn
  • 34,664
  • 13
  • 111
  • 163
  • Yes I know of this 'solution' (hack) but I was hoping HippoMock author would comment on the necessity to implicitly manage the deallocation of mocks. BTW, you mean m_EmpSvcMock = make_shared_ref(empSvcMock); – Zach Saw Aug 02 '12 at 07:51
2

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.

dascandy
  • 7,184
  • 1
  • 29
  • 50
  • So when passing mock to a shared_ptr, we should simply register a NOP destructor? Could you give me an example please? – Zach Saw Aug 02 '12 at 07:57
  • When passing it to a shared pointer you expect the destructor to be called at the end of your test but before the mocks are verified. So, tell that to the mock repository and you're fine. This is the unit test for it http://www.assembla.com/code/hippomocks/git/nodes/master/HippoMocksTest/test_zombie.cpp which explicitly deletes it, but that's equivalent to the last shared pointer being destructed. – dascandy Aug 02 '12 at 08:03
  • @dascandy: This is interesting! From the unittest, it looks like the Git version should just work with shared_ptr. – Zach Saw Aug 02 '12 at 08:06