chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hi

I have the following function I would like to test:

bool QualityLib::isGood(int quality)
{
MSC_NAMESPACE::ObjectQuality objectQuality;
objectQuality.setValue(quality);

return objectQuality.isGood();
}

Here is my test:

void tst_QualityLib::isGood()
{
ObjectQuality *fakeObjectQuality = FAKE_ALL<ObjectQuality>();
WHEN_CALLED(fakeObjectQuality->isGood()).ReturnVal(true);

const int QUALITY = 192;

QualityLib qualityLib(0);

bool result = qualityLib.isGood(QUALITY);
ASSERT_WAS_CALLED (fakeObjectQuality->setValue(_));
ASSERT_WAS_CALLED (fakeObjectQuality->isGood());
QCOMPARE(result, true);
}

How to I test that the value 192 was actually passed to setValue() ? Also can I use ASSERT_WAS_CALLED to test the ObjectQuality constructor, istead of using my setValue method?

Regards,
James
asked by JamesKing (8.2k points)

2 Answers

0 votes
Hi James,

In order to check arguments values you can use the DoInstead API.
So in your case it can be something like this:

class Helper
{
public:
void FakeSetValue(int val);
};

void Helper::FakeSetValue(int val)
{
ASSERT_EQ(192, val);
}
// test code
Helper h;
WHEN_CALLED(fakeObjectQuality->setValue(0)).DoMemberFunctionInstead(&h, FakeSetValue);
answered by Shai Barak (1.5k points)
0 votes
Thanks for the detailed answer!
answered by JamesKing (8.2k points)
...