CallOriginal Behavior

Top  Previous  Next

Isolator++ Professional allows you to fake methods on live objects, but there are scenarios where you might want to call the original implementation for certain methods while keeping the faked behavior for others.

This can be useful when you want to simulate real behavior for some methods of the faked object and use fakes for others.

 

class Person

{

public:

   int GetAge() { return 10; }

   string GetName() { return "Mr. Jones"; } 

};

 

TEST_F(Examples, CallingOriginalImplementationOnFakedObject)

{

   // Arrange

   auto a = Isolator();  

   Person* fakePerson = a.Fake.Instance<Person>();

  

   a.CallTo(livePerson->GetAge()).WillCallOriginal();

 

   // Act

   auto result = fakePerson->GetAge();  

 

   // Assert

   ASSERT_EQ(10result); // real implementation

   ASSERT_STREQ("", fakePerson->GetName()); // fake implementation

}

 

 

Calling the original and returning a fake result

 

To call a method and then return another fake result use the following API:

 

 

 a.CallTo(livePerson->GetAge()).WillCallOriginal().AndThenReturn(5);

 

 

Note: To catch an exception in the test thrown in a CallOriginal method on a 64-bit system in your test, use the HandleExceptionInTest flag. For more details, see Handling Exceptions thrown by fakes.

 


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