Faking static methods

Top  Previous  Next

With Isolator++ Professional,you can easily change the behavior of static methods using the a.CallTo API. This allows you to test code that depends on static methods without requiring modifications to the original implementation.

 

Consider the following class with a static method:

 

class MyClass

{

   public  static int GetResult() { return -1; }

...

 

To change the behavior of GetResult, for example to return another value, say 10, you can write:

 

 

auto a = Isolator();  

a.CallTo(MyClass::GetResult()).WillReturn(10);

 

 

 

We use the CallTo API to declare the behavior we want, this time with the static signature of the method. In this case, we return 10 instead of -1.

 

If you want to fake all public static methods of a class and have them return default values (see Recursive Fakes), you can use the Fake.Statics API:

 

 

a.Fake.Statics<MyClass>();

 

 

 

 

Additional Reference:

See Faking private and protected methods to change the behavior of a private static method.

 


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