One of the benefits of dependency injection is ease of testing, as mocked classes can be injected. Clazz takes raw pointer for that purpose and moves it to unique pointer, to signal that it owns InjectedObj object:
Clazz::Clazz(InjectedObj *injectedObj) : injectedObjPtr(injectedObj) { }
where injectedObjPtr
is member:
std::unique_ptr<InjectedObj> injectedObjPtr;
doSth method calls executeSth by calling smart pointer:
//doSth method
int CLazz::doSth() {
return injectedObjPtr->executeSth();
}
I would like to test Clazz by setting some expectations on injected object, so my test looks similar to this:
TEST_F(testFixture, exeuteTest)
{
//before
InjectedObj* injectedObj = new InjectedObj();
EXPECT_CALL(*injectedObj, executeSth())
.Times(1)
.WillOnce(Return(100));
//when
Clazz clazz(injectedObj);
//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
Clazz::Clazz()
takes a raw pointer and store it intostd::unique_ptr<>
; 2.std::shared_ptr<Clazz> clazz(injectedObj);
directly pass the raw pointer tostd::shared_ptr<>
. – Velvavelvetoperator->
inClazz
? What it returns? – Peritoneum