Invoking Public and Private Methods |
Top Previous Next |
In some cases, you may need to interact with private members of a class to test certain behaviors or manipulate the state of an object for your tests. Isolator++ Professional provides a powerful InvokeOriginal API that allows you to call private instance members, static methods, and constructors, even if they are normally inaccessible.
Note: The InvokeOriginal method will always execute the original implementation, even if the method has been faked, and it is configured with the HandleExceptionInTest flag. For more information, refer to Handling Exceptions Thrown by Fakes. Methods that are called from the Invoked Methods will be Faked if they are set this way
To call a private instance member:
auto a = Isolator();
auto ret = a.InvokeOriginal<int>(A::Member(&instance, PrivateMemeber), args...);
You can also call private static methods using A::Global to specify the method you want to invoke.
auto ret = a.InvokeOriginal<int>(A::Global(MyClass::PrivateMemeber), args...);
For static C functions, you can use A::Global to call them in a similar fashion to private static methods:
auto ret = a.InvokeOriginal<int>(A::Global(staticFunctionInModule), args...);
You can also call constructors for faked instances. This is useful for situations where you want to initialize an object but keep its behavior faked.
Person* fakePerson = a.Fake.Instance<Person>(); // Call constructor, Init is Faked by default, you can change any behavior before calling the constructor a.InvokeOriginal(A::Ctor(fakePerson)
If you don't care about the return value or want to invoke a void method, you can omit the return type template:
a.InvokeOriginal(A::Member(&instance, PrivateMemeber), args...);
|
Copyright Typemock Ltd. 2009-2025. All Rights Reserved.