Faking "future" instances of classes

Top  Previous  Next

In some cases, the dependency you want to fake is instantiated inside the constructor or method of the class-under-test.

These instances, created during the execution of the test, are referred to as future instances. To fake such dependencies, Isolator++ Professional provides the Fake.All API.

 

Consider a Person class that creates an Address object within its constructor. The Address object interacts with the database, which we want to avoid during testing.

 

 
class Person

{

private:

   Address* address;

public:

   Person() { address = new Address(); 

   char* GetCity() {return address->GetCity();}

  

}

 

Here, the Address object is created after the Person constructor is called, making it a future instance.

To intercept and fake the Address object, we use the Fake.All API. This creates a fake handle for all future Address instances, allowing us to define custom behavior.
 

TEST_F(PersonTests, FakeAddress)

{

   // Arrange

   auto a = Isolator();  

   auto addressHandle = a.Fake.All<Address>();

 

   Person person;

   

   a.CallTo(addressHandle->GetCity()).WillReturn("NYC");

 

   // Act

   auto result = person.GetCity();  

 

   // Assert

   ASSERT_EQ("NYC", result);

}

 

Explanation:

 

1. Intercepting Future Instances:

The a.Fake.All<Address>() call creates a fake handle that controls all future instances of Address.

2. Defining Behavior:

Use CallTo on the fake handle (addressHandle) to define behavior for methods of Address.

3. Testing the Class-Under-Test:

When the Person constructor creates an Address object, Typemock intercepts the instantiation and replaces it with a fake Address controlled by the handle.

4. Behavior Across Instances:

All future Address objects created during the test will follow the behavior defined by the fake handle.

 

 

Enterprise Feature – Controlling and Verifying Future Constructor Calls

 

For Isolator++ Professional Enterprise users, Fake.All can also control the behavior of constructors for future instances.

This allows you to:

·Ignore specific constructor calls so that object fields remain uninitialized (helpful for avoiding heavy initialization logic).
·Conditionally call the original constructor based on argument matching, enabling chaining scenarios where some constructors are skipped and others run normally.
·Verify that a specific base class constructor was invoked when initializing a derived class object.

 

This test demonstrates controlling future constructor calls:

1.The first matching ctor call is ignored (fields remain uninitialized ⇒ "NE").
2.Other calls fall back to the original constructor (per FakeOptions::CallOriginal).

 

TEST(EnterpriseExamples, FutureCtor_ConditionalIgnoreAndCallOriginal)

{

    // Arrange

    auto a = Isolator();

    auto handle = a.Fake.All<GPSLocation>(FakeOptions::CallOriginal);

 

    // Ignore a specific constructor invocation by argument match

    a.CallToPrivate(A::Ctor(handle), A::Eq(1), A::Eq(2)).WillBeIgnored();

 

    // Act

    auto ignored = new GPSLocation(1, 2);      // ctor ignored

    auto original = new GPSLocation(10, 10);   // ctor runs (default: CallOriginal)

 

    // Assert

    // First call: values are uninitialized (not equal to provided args)

    ASSERT_NE(1, ignored->Latitude());

    ASSERT_NE(2, ignored->Longitude());

 

    // Second call: original ctor executed, values match

    ASSERT_EQ(10, original->Latitude());

    ASSERT_EQ(10, original->Longitude());

}

 

 

This test verifies that a base-class constructor is invoked when creating a derived instance, note the use of the template specification in A::Ctor.

 

TEST(EnterpriseExamples, FutureCtor_VerifyBaseConstructorCalled)

{

    // Arrange

    auto a = Isolator();

    auto handle = a.Fake.All<SecretGPSLocation>(FakeOptions::CallOriginal);

 

    // Act

    auto derived = new SecretGPSLocation();

 

    // Assert

    // Verify base (GPSLocation) ctor was invoked with any arguments

    a.CallToPrivate(A::Ctor<GPSLocation>(handle), A::Any()A::Any()).VerifyWasCalled();

}

 

These capabilities make it possible to test classes that internally chain constructors or depend on base-class initialization, without executing the full construction logic every time.

 

Note: This feature is available only in the Enterprise edition of Isolator++ Professional.

 

It is possible to set a different default behavior of the future fakes by using the Fake.All<>(FakeOption::<option>), see Setting Default Behavior.


Copyright  Typemock Ltd. 2009-2025.  All Rights Reserved.