Interested in a
Personal Demo ?


Name* :
Please Enter your Name
Company E–Mail* :
Please Enter a Valid Email

Testimonials


"Typemock - My testing tool of choice. Each time I use it, I rediscover its power, and I'm amazed. Great tool!"
Jason Divis, @CodingOfficer
Success stories

PurchaseOptions


US Toll Free
866–895–4680
Outside US
+44–203–150–0293
Get your printable quote
Buy online

 « Asserting a function call | Main | Getting started »

How To Change Behavior With Isolator++

In this example, I’m using GTest as my test framework. I use the TEST_F macro for declaring a test, with the class name and scenario.

TEST_F(IsolatorTests, ChangeReturnedIntValue)

With Isolator you can change behavior of any calls, instance or statics. In order to fake calls, you still need to create a fake class using the FAKE macro:

ConcreteClass* fakeConcrete = FAKE(ConcreteClass);

My ConcreteClass is a regular class, and I would like to change the behavior of its GetInt() method. The concrete implementation returns -1. By default, native return types like int, doubles, bools return their default values. So if you want to return these values, using the FAKE macro takes care of that. But if you want to change the return value, you can use the WHEN_CALLED macro, like this:

WHEN_CALLED(fakeConcrete->GetInt()).Return(5);

The macro takes the function call to change, and is completed by Return to specify the return value.

I’m completing this test with an ASSERT_EQ (GTest macro for asserting equality) to make sure that the GetInt call returns what I specified:

ASSERT_EQ(5, fakeConcrete->GetInt());

If GetInt returns something other than 5, the test will break when run.

void methods are ignored by default; so again, there’s no need to specify this behavior. However, you can tell the fake instance, that for specific methods, it should run its original implementation:

WHEN_CALLED(fakeConcrete->GetInt()).CallOriginal();

With this, GetInt returns it’s original value, -1.

If you’d like to throw an exception (this is a TypemockException), you’ll use the Throw function:

WHEN_CALLED(fakeConcrete->GetInt()).Throw("Exception occured");

This is valuable when you want to simulate an exception thrown from a dependency.