Interested in a
Personal Demo ?

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


"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


US Toll Free
Outside US
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:


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:


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.