Faking abstract classes and pure virtual methods

Top  Previous  Next

With Isolator++ Professional, you can easily fake the behavior of any class, including abstract classes, using the a.Fake API.

 

Example: Changing the Behavior of a Virtual Method

Consider the following abstract class:

 

class MyPureClass

{

public:

   virtual int GetResult() = 0;

}

 

To fake the GetResult() method and change its behavior:

 

 

auto a = Isolator();  

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

a.CallTo(fakeMyClass->GetResult()).WillReturn(10);

 

 

 

First, we create a fake object using the Fake API. This fakes all MyClass methods. Primitives return 0, and methods returning objects return auto generated fake objects. (see Recursive Fakes).

To further change specific methods, we used the CallTo API to declare the behavior we want. In this case, we return the value 10.

 

Note: Isolator++ Professional will automatically fake all the methods in the hierarchy.

 

 

Note: In some cases, the compiler may optimize out virtual methods, which can prevent Isolator++ from faking them. To address this, you can use the Testable.Members<> API to ensure the compiler retains the virtual methods.

 

 

auto a = Isolator();  

a.Testable.Members<MyPureClass>();

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

 

 

and

 

 

auto a = Isolator();  

a.Testable.Members<MyPureClass>();

auto fakeMyClass = a.Fake.All<MyPureClass>();

 


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