lundi 23 juillet 2018

GMOCK EXPECT_CALL on unique_ptr

Is there any way of mocking calls to object that is referenced using unique_ptr? My mocked class gets injected to main class in constructor:

Clazz::Clazz(InjectedObj *injectedObj)
 : injectedObjPtr(injectedObj)
{
}

where injectedObjPtr is member:

std::unique_ptr<InjectedObj> injectedObjPtr;

Next my test looks similar to this:

TEST_F(testFixture, exeuteTest)
{
  //before
  InjectedObj* injectedObj = new InjectedObj();
  std::shared_ptr<Clazz> clazz(injectedObj);
  EXPECT_CALL(*injectedObj, executeSth())
    .Times(1)
    .WillOnce(Return(100));

  //then
  ASSERT_DOUBLE_EQ(clazz->doSth(), 100);
}

So in this simplified scenario clazz->doSth() is calling injectedObj->executeSth which should return 100, but it behaves like I would never set my expectation and always gets 0. Obviously if I will call my mocked object without smart pointer: injectedObj->executeSth it returns 100, but not when calling it using unique_ptr. Is there any way of telling gmock to set correct expectations when mocked object is managed by smart pointer? Thanks

Aucun commentaire:

Enregistrer un commentaire