Controlling Non-Public Methods

You can control non-public methods by doing the following:

Faking private instance methods.

Faking private static methods

Faking private properties and indexers

Creating instances of types with private constructors

When to Use

When your test requires a specific return value from a non-public fake method.

Public methods are also supported with Isolate.NonPublic. This enables you to change the visibility of your methods without changing your tests.

Faking Private Methods

To fake private methods, use the Isolate.NonPublic. This API uses a string name of the method that you want to fake. Typemock Isolator will find the method through reflection and arrange the new behavior. To fake an instance method, pass the instance and the name of the method.

To allow production code to change its method scope without failing unit tests, Isolate.NonPublic can be used for public methods too.

Syntax
C#

Isolate.NonPublic.WhenCalled(<instance>, "<methodname>").<behavior>

VB

Isolate.NonPublic.WhenCalled(<instance>, "<methodname>").<behavior>

The following table explains possible behavior:

Behavior

Description

ReturnRecursiveFakes

Returns:

For reference types: fake objects

For other return types: zero or equivalent


The returned fake objects will behave in the same way.

WillReturn

Returns a specified value.

WillThrow

An exception will be thrown when this method will be called.

WillBeIgnored

The method will not be executed.

CallOriginal

Calls the original method.

AssignRefOut

Additional information for Ref and Out parameters to be returned by the called method.

Note that the only difference from faking public methods is the use of string names to represent the method to fake.

Sample 1:
C#

var fakeDependency = Isolate.Fake.Instance<Dependency>(Members.CallOriginal);
Isolate.NonPublic.WhenCalled(fakeDependency, "InternalNumber").WillReturn(3);

VB

Dim fakeDependency = Isolate.Fake.Instance(Of Dependency)(Members.CallOriginal)
Isolate.NonPublic.WhenCalled(fakeDependency, "InternalNumber").WillReturn(3)

Sample 2: Returning enum value

To set a different behavior to method, which returns hidden enum, use WhenReturn() with an underlying value.

C#
        
[TestMethod]
public void FakeMethodReturningPrivateEnum()
{
  var instance = new ClassUnderTest();
  Isolate.NonPublic.WhenCalled(instance, "GetEnumValue").WillReturn(64); // DayOfWeek.Saturday
  
  var result = instance.IsWeekend();
  
  Assert.IsTrue(result); 
}

public class ClassUnderTest
{
  public bool IsWeekend()
  {
    var value = GetEnumValue();
    
    if ((value & (DaysOfWeek.Sunday | DaysOfWeek.Saturday)) > 0)
      return true;
    
    return false;
  }
  
  private DaysOfWeek GetEnumValue()
  {
    throw new NotImplementedException();
  }
  
  [Flags]
  private enum DaysOfWeek
  {
    None = 0,
    Sunday = 1,
    Monday = 2,
    Tuesday = 4,
    Wednesday = 8,
    Thursday = 16,
    Friday = 32,
    Saturday = 64
  }
} 
  

VB

<TestMethod()>
Public Sub FakeMethodReturningPrivateEnum()
  Dim instance = New ClassUnderTest()
  Isolate.NonPublic.WhenCalled(instance, "GetEnumValue").WillReturn(64) 'DayOfWeek.Saturday
  
  Dim result = instance.IsWeekend()
  
  Assert.IsTrue(result)
End Sub

Public Class ClassUnderTest
  Public Function IsWeekend() As Boolean
    Dim value = GetEnumValue()
    If ((value & (DaysOfWeek.Sunday Or DaysOfWeek.Saturday)) > 0) Then
      Return True
    End If
    Return False
  End Function
  
  Private Function GetEnumValue() As Object
    Throw New NotImplementedException()
  End Function

  <Flags>
  Private Enum DaysOfWeek
    None = 0
    Sunday = 1
    Monday = 2
    Tuesday = 4
    Wednesday = 8
    Thursday = 16
    Friday = 32
    Saturday = 64
  End Enum
End Class