On 2012-09-11 11:25, Bogdan Spuze wrote:
> Thanks for the information Martin.
>
> I don't wanna do anything in the constructor(I used this terms because
> I thought that it would be easier to explain).
>
> So. I have a mocked class. There are two functions in it: Init() and
> Destroy().
>
> Just wanna check that only after Init() is called then Destroy() will
> be called also.
> Somethig like
>
> if( Init() ) //if init() is called
> EXPECT_CALL(mock_obj, Init()).Times(1);
> else
> EXPECT_CALL(mock_obj, Destroy()).Times(0);
Ok, Bogdan,
If you make it a StrictMock, it fails on unexpected calls. Something
like this (not tried):
class MyMock
{
MOCK_METHOD0(Init);
MOCK_METHOD0(Destroy);
};
class MyFake:
public testing::StrictMock <MyMock>
{
void expectInitAndDestroy() { EXPECT_CALL(*this,
Init()).WillOnce(InvokeWithoutArgs(this, expectDestroy)); }
void expectDestroy() { EXPECT_CALL(*this, Destroy()); }
};
TEST(TargetIsDestroyedAfterInit)
{
MyFake myFake;
myFake.expectInitAndDestroy();
MyObject myObject(myFake);
}
But fakes do not make Good Unit Tests (GUTs). It's much better to just
expect both calls from the test case.
TEST(TargetIsInitedAndDestroyed)
{
MyFake myFake;
{
testing::InSequence;
myFake.expectInit();
myFake.expectDestroy();
}
MyObject myObject(myFake);
}
/Martin Svensson