Invoking Private Methods

To invoke private methods, use Isolate.Invoke.Method().

When to Use

You can invoke private methods when your test requires a private method to be called.

Syntax

C#

Isolate.Invoke.Method(<instance>, "<method_name>", <list_of_arguments>);

VB

Isolate.Invoke.Method(<instance>, "<method_name>", <list_of_arguments>)

Samples

Sample 1: Invoking an Instance Private Method

The following sample shows how to invoke an instance private method. Arguments which are passed to the method from Isolate.Invoke.Method() are 2 and 5.

C#

[TestMethod, Isolated]
public void InvokePrivateMethod()(
{
  var underTest = new ClassUnderTest();
  var result = Isolate.Invoke.Method((underTest, "Sum", 2, 5);
  Assert.AreEqual(7, result);
}

VB

<TestMethod, Isolated>
Public Sub InvokePrivateMethod()

  Dim underTest = New ClassUnderTest()
  Dim result = Isolate.Invoke.Method((underTest), "Sum", 2, 5)

  Assert.AreEqual(7, result)

End Sub

Sample 2: Calling a Base Method

You can use Isolate.Invoke.Method() to call non-public methods that are defined in a base class of the object. To call an overridden method in the base class, use Isolate.Invoke.MethodFromBase().

The following sample shows how to a base method (this works for both hidden and virtual methods).

C#

//Syntax
Isolate.Invoke.MethodFromBase<what-base-type>(<instance>,"method-name", list-of-args);

[TestMethod, Isolated]
public void InvokeBaseMethod()
{
  var underTest = new ClassUnderTest();
  var result = Isolate.Invoke.MethodFromBase<ClassUnderTest>(underTest,"Subtract", 5, 2);
  Assert.AreEqual(3, result);
}

VB

'Syntax
Isolate.Invoke.MethodFromBase(Of <what-base-type>)(<instance>,"method-name", list-of-args)

<TestMethod, Isolated>
Public Sub InvokeBaseMethod()
    Dim underTest = New ClassUnderTest()
    Dim result = Isolate.Invoke.MethodFromBase(Of ClassUnderTest)(underTest, "Subtract", 5, 2)
    Assert.AreEqual(3, result)
End Sub

Sample 3: Invoking a Static Method
C#

//Syntax
Isolate.Invoke.Method<type>("method-name", list-of-args);

[TestMethod, Isolated]
public void InvokePrivateStaticMethod()
{
  var result = Isolate.Invoke.Method<ClassUnderTest>("Multiply", 2, 5);
  Assert.AreEqual(10, result);
}

VB

'Syntax
Isolate.Invoke.Method(Of <type>)("method-name", list-of-args)

<TestMethod, Isolated>
Public Sub InvokePrivateStaticMethod()
    Dim result = Isolate.Invoke.Method(Of ClassUnderTest)("Multiply", 2, 5)
    Assert.AreEqual(10, result)
End Sub

Sample 4: Passing Arguments

When a method has arguments that need to be passed as Ref or Out parameters, you can use the Args class to create the arguments. The following sample shows how to pass three arguments as follows:

One argument is passed by value

One argument is passed as a Ref parameter

One argument is passed as an Out parameter

To read the returned value the IBox<int> property is used.

C#

[TestMethod, Isolated]
public void InvokeMethod_WithIntRefAndOutArgument_RefAndOutValuesAreAssigned()
{
  var t = new ClassWithPrivateMethods();
  IBox val1 = Args.Ref(10);
  IBox val2 = Args.Out<int>();
  var result = (int)Isolate.Invoke.Method(t, "MethodWithIntRefAndOutArgument", 10, val1, val2);
  Assert.AreEqual(result, 4);
  Assert.AreEqual(val1.Value, 1);
  Assert.AreEqual(val2.Value, 2);
}

VB

<TestMethod, Isolated>
Public Sub InvokeMethod_WithIntRefAndOutArgument_RefAndOutValuesAreAssigned()
    Dim t = New ClassWithPrivateMethods()
    Dim val1 As IBox(Of Integer) = Args.Ref(10)
    Dim val2 As IBox(Of Integer) = Args.Ref(2)
    Dim result = CInt(Isolate.Invoke.Method(t, "MethodWithIntRefAndOutArgument", 10, val1, val2))
    Assert.AreEqual(result, 4)
    Assert.AreEqual(val1.Value, 1)
    Assert.AreEqual(val2.Value, 2)
End Sub

There is no "out" Parameter in VB

Sample 5: Handling Overloaded Methods Passing Null Values as Parameters

Typemock Isolator automatically calls the correct method based on the arguments you passed. However, when nulls are sent, you cannot collect that information from the passed arguments. To be able to collect this information, use Args.Null().

The following sample shows that in order to pass values for Nullable types as parameters, the type is specified using Args.Null().

C#

[TestMethod, Isolated]
public void InvokeMethod_CreateNullArgumentForOverloadedMethod_NullableType()
{
  var result= Isolate.Invoke.Method<ClassUnderTest>("MultiplyNullable", 2, Args.Null<int?>());
  
  Assert.AreEqual(0, result);

VB

<TestMethod, Isolated>
Public Sub InvokeMethod_CreateNullArgumentForOverloadedMethod_NullableType()
    Dim nullParamToPrivateMethod = New NullParamToPrivateMethod()
    Dim result = CInt(Isolate.Invoke.Method(nullParamToPrivateMethod, "CallMe", Args.Null(Of System.Nullable(Of Integer))()))
    Assert.IsTrue(result = 1)
End Sub

Sample 6: Passing Ref Arguments

To pass Ref or Out parameters, use Args.Out or Args.Ref as follows:

1. Create a Ref or Out object

2. Pass it to the method

3. Get the value.

C#

[TestMethod, Isolated]
public void InvokePrivateMethodWithRef()
{
  var byRef = Args.Ref(5);
  Isolate.Invoke.Method<ClassUnderTest>("MultiplyByRef", byRef, 2);
  var result = byRef.Value;
  Assert.AreEqual(10, result);
}

VB

<TestMethod, Isolated>
Public Sub InvokePrivateMethodWithRef()
    Dim byRefArg = Args.Ref(5)
    Isolate.Invoke.Method(Of ClassUnderTest)("MultiplyByRef", byRefArg, 2)
    Dim result = byRefArg.Value
    Assert.AreEqual(10, result)
End Sub