Isolator++ has two ways to return stuff for a method. It has the ReturnVal to return values, and ReturnPtr to return pointers to objects. What about returning a class by value?
Though there’s no API (currently), it’s very easy to do. You use the almighty 10lbs hammer known as DoInstead.
Let’s say you have this method:
bool Person::IsLivingInLA()
{
Address currentAddress = Address::GetAddress();
if (strcmp (currentAddress.GetCity(),“Los Angeles”)==0)
{
return true;
}
else
return false;
}
The GetAddress method returns an Address object by value. How can we return our own object?
Let’s create a helper class with a static method with the same signature of GetAddress:
1 2 3 4 5 6 7 8 9 |
class TestUtil { public: static Address ReturnAddress() { |
1 2 3 4 5 |
Address laAddress; laAddress.SetCity( "Los Angeles"); return laAddress; } }; |
This class is part of the test project, not the production code. Now, in the test, we’ll fake the static method GetAddress, by calling the ReturnAddress method instead.
1 2 3 4 |
TEST_F(PersonTests, IsLivingInLA_AddressIsLA_ReturnsTrue) { Person person; TestUtil testUtil; |
// Fake the static method by calling the other method instead
1 2 3 4 5 6 |
FAKE_STATICS<Address>(); WHEN_CALLED(Address::GetAddress()). DoStaticOrGlobalInstead(TestUtil::ReturnAddress, NULL); ASSERT_TRUE(person.IsLivingInLA()); ISOLATOR_CLEANUP(); } |
Using the DoStaticOrGlobalInstead, I pass the static method replacement, and a NULL value (which can be a context if needed, but not needed in this case).
Remember that another option, for calling DoInstead on a member function, you’ll use the call the DoMemberFunctionInstead method instead. That’s because it needs the same signature.