Assume I have the following class:
class WeirdCalculator
{
public:
WeirdCalculator() { };
int addWithOffset(int a, int b)
{
OffsetProvider provider;
if (provider.applyOffset())
return a + b + provider.getOffset();
else
return a + b;
};
}
Further assume that OffsetProvider::applyOffset()
and OffsetProvider::getOffset()
not necessary always return the same values (applyOffset()
is false
on leap-years, and getOffset()
normally returns 42
but yields a different value on Monday 13th).
Putting this class under unit-test (so you can refactor it), you will quickly notice that it is hard to cover the else
path (but you can do it with some trickery). Looking closer you also notice that the result of addWithOffset()
is dependent on the result of getOffset()
.
Should your test coverage include those possible divergence points (i.e. also simulate a test in leap years and on Monday 13th)? The impending refactorings might render those tests soon obsolete, but if you do not cover them you might not spot that you accidently hardcoded that 42
instead of using the value from getOffset()
.