Setting Default Behavior of a Faked Class

Top  Previous  Next

When faking a class, you can set the default behavior of all methods. You can also set a different behavior for a specific method.

 

auto a = Isolator();  

auto fakeMyClass = a.Fake.Instance<MyPureClass>(FakeOption::<option>);

 

Fake Option

Behavior

FakeOption::Recursive

This is the default behavior when faking an object. 

Methods will return:

·For object types: Fake objects
·For other return types: Zero or equivalent

This ensures that returned values will never be null
(see Recursive Fakes)

FakeOption::CallOriginal

Calls to fake object methods will pass through the original implementation. Constructor is called during object's instantiation.

Useful when you want to maintain original behavior and modify specific methods or verify that calls were made.

FakeOptions::HandleExceptionInTest

Tells Isolator++ that exceptions are caught in the test code as opposed to be caught in the under test code.

Note: This is for 64 bit only.
(See Handling Exceptions thrown by fakes)

 

Example use of the HandleExceptionInTest flag with a Private method:

 

TEST_F(WhenToUse, HandleExceptionInTest)

{

   // Arrange

   // create a fake under test that will call all original code but exceptions will be caught in this test

   auto a = Isolator();  

   auto underTest = a.Fake.Instance<UnderTest>(FakeOption::CallOriginal | FakeOptions::HandleExceptionInTest); 

 

   try

   {

            // Act

       a.Invoke(A:Member(&underTest, PrivateMethodThatWillThrow));

       // Assert

       ASSERT_EQ(TRUE, FALSE); // fail test we expected an excpetion

   }

   catch (MyException& e)

   {

  // Assert

       // can verify exception if needed

   }

}


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