
|
What is Isolator++?Typemock Isolator++ is a new unit testing tool for C++. With its API, you can test your C++ code, and isolate the code under test from dependencies (like other classes). Let’s walk through an example to show what that means.
Let’s say I have a Product class. This class has a GetPrice() function:
int Product::GetPrice(PricingManager* manager)
{
if (manager->isOnSale())
return 5;
else
return 10;
}
As you can see, the GetPrice() function has a dependency: the PricingManager pointer it receives as an argument. The PricingManager’s isOnSale() method looks like this:
bool PricingManager::isOnSale()
{
return false;
}
It always returns false. I want to test my Product, with both cases – when we have a sale or not. But I don’t want to change the code of isOnSale() just for a test. Maybe I can’t touch it. This is where Isolator++ helps. It allows you to change the behavior of dependencies, without changing the code. I’ll create two tests (for a step-by-step guide, read the “Getting Started” section). For the first scenario, where there is no sale, here’s the test:
TEST_F(IsolatorTests, GetPrice_OnSaleIsFalse_ExpectHighPrice)
{
Product product;
PricingManager* manager = new PricingManager();
ASSERT_EQ(10, product.GetPrice(manager));
}
Since manager->isOnSale() always returns false, the GetPrice() function always returns 10. However, the second case is different. I want manager->isOnSale() to return true, but without changing the code. Here’s how the test looks like:
TEST_F(IsolatorTests, GetPrice_OnSaleIsTrue_ExpectLowPrice)
{
Product product;
PricingManager* fakeManager = FAKE(PricingManager);
WHEN_CALLED(fakeManager->isOnSale()).Return(true);
ASSERT_EQ(5, product.GetPrice(fakeManager));
}
I’m using Isolator++’s FAKE macro to create a fake a PricingManager instance (although from a type point of view, this is a regular PricingManager pointer). After that, I’m using Isolator++ to specify the return value true, I want the isOnSale method to return. Finally, I’m testing the expected return value 5. Isolator++ has many more features. You can learn more in other posts coming up. |