Verifying Non-Public Methods

When to Use

When you want to check a case when a specific private method is called.

To verify that a non-public method was called, use Isolate.Verify.NonPublic() with one of the following verification statements:

Statement

Description

WasCalled

Passes the verification if during the test execution, the call was made at least once.

WasNotCalled

Passes the verification if the call was never made.

To verify properties and indexers, add the following before the verification statement:

Behavior Type

Description

Property

Specialized API for properties.

Indexer

Specialized API for indexers.

Syntax

Method Type

API

Instance

instance Isolate.Verify.NonPublic.WasCalled(<instance>, "PrivateMethodname");

Static

Isolate.Verify.NonPublic.<WasCalled>(typeof(Dependency), "PrivateMethodname");.

Static Method Generic

Isolate.Verify.NonPublic.WasCalled(typeof(<type>), "<method>", <list of generic types>);

C#

//Static
Isolate.Verify.NonPublic.<WasCalled>(typeof(Dependency), "PrivateMethodname");

//Instance
Isolate.Verify.NonPublic.WasCalled(<instance>, "PrivateMethodname");

//Property
Isolate.Verify.NonPublic.Property.WasCalledGet(realDependency, "PrivateProp");

//Indexer
Isolate.Verify.NonPublic.Indexer.WasCalledGet(realDependency);

VB

'Static
Isolate.Verify.NonPublic.WasCalled(GetType(Dependency), "PrivateMethodname")

'Instance
Isolate.Verify.NonPublic.WasCalled(<instance>, "PrivateMethodname")

'Property
Isolate.Verify.NonPublic.Property.WasCalledGet(realDependency, "PrivateProp")

'Indexer
Isolate.Verify.NonPublic.Indexer.WasCalledGet(realDependency)

Public methods are also supported with the Isolate.NonPublic API. This allows you to change the visibility of your methods without having to change the tests.

Samples

Sample 1: Verifying the Non-Public Method Was Called

The following sample shows how to verify that the non-public method was called.

C#

[TestMethod, Isolated]
public void VerifyPrivateStaticMethod_WasCalledWithAnyArg()
{
  Isolate.NonPublic.WhenCalled<Dependency>("CallGuard").IgnoreCall();
  var classUnderTest = new ClassUnderTest();
  var result = classUnderTest.Calculate(1, 2);
  Isolate.Verify.NonPublic.WasCalled(typeof (Dependency), "CallGuard");
}

VB

<TestMethod, Isolated>
Public Sub VerifyPrivateStaticMethod_WasCalledWithAnyArg()
    Isolate.NonPublic.WhenCalled(Of Dependency)("CallGuard").IgnoreCall()
    Dim classUnderTest = New ClassUnderTest()
    Dim result = classUnderTest.Calculate(1, 2)
    Isolate.Verify.NonPublic.WasCalled(GetType(Dependency), "CallGuard")
End Sub
Sample 2: Verifying the Property and Indexer Was Called

The following sample shows how to verify the property and indexer.

C#
[TestMethod, Isolated]
public void VerifyingPropertyAndIndexerWasCalled()
{
  //Arrange
  var fake = Isolate.Fake.Instance<Dependency>(Members.CallOriginal);
  Isolate.NonPublic.Property.WhenGetCalled(fake, "PrivateProperty").WillReturn(10);
  Isolate.NonPublic.Indexer.WhenSetCalled(fake).IgnoreCall();
  //Act here
  ...
  //Assert
  Isolate.Verify.NonPublic.Property.WasCalledGet(fake, "PrivateProperty"); //verification for private get property
  Isolate.Verify.NonPublic.Indexer.WasCalledSet(fake); //verification for private set indexer
}

VB

<TestMethod, Isolated>
Public Sub VerifyingPropertyAndIndexerWasCalled()
    'Arrange
    Dim fake = Isolate.Fake.Instance(Of Dependency)(Members.CallOriginal)
    Isolate.NonPublic.Property.WhenGetCalled(fake, "PrivateProperty").WillReturn(10)
    Isolate.NonPublic.Indexer.WhenSetCalled(fake).IgnoreCall()
    
    'Act here
     ...
  
    'Assert
    Isolate.Verify.NonPublic.Property.WasCalledGet(fake, "PrivateProperty")
    'verification for private get property
    Isolate.Verify.NonPublic.Indexer.WasCalledSet(fake)
    'verification for private set indexer
End Sub
Sample 3: Verifying that the Private Method Was Called With Specific Arguments

Typemock Isolator does not match arguments of the verified called methods. To verify that a private method was called with a specific arguments, use Isolate.Verify.NonPublic.WasCalled.WithArguments() and pass the matching arguments.

C#
//Syntax
Isolate.Verify.NonPublic.WasCalled((typeof(fake), "PrivateMethod").WithArguments(<arguments>);

[TestMethod, Isolated]
public void VerifyPrivateMethod_WasCalledWithExactArg_Example()
{
  Isolate.NonPublic.WhenCalled<Dependency>("CallGuard").IgnoreCall();
  var result = new ClassUnderTest().Calculate(1, 2);
  Isolate.Verify.NonPublic.WasCalled(typeof(Dependency), "CallGuard").WithArguments("typemock", "rocks");
}

VB

'Syntax
Isolate.Verify.NonPublic.WasCalled(GetType(fake), "PrivateMethod").WithArguments(<arguments>)

<TestMethod, Isolated>
Public Sub VerifyPrivateMethod_WasCalledWithExactArg_Example()
    Isolate.NonPublic.WhenCalled(Of Dependency)("CallGuard").IgnoreCall()
    Dim result = New ClassUnderTest().Calculate(1, 2)
    Isolate.Verify.NonPublic.WasCalled(GetType(Dependency), "CallGuard").WithArguments("typemock", "rocks")
End Sub

Sample 4: Verifying that a Private Static Generic Method Was Called
C#

[TestMethod, Isolated]
public void VerifyPrivateGenericMethod_WasCalled()
{
  Isolate.NonPublic.WhenCalled<Dependency>("PrivateCallGuardGeneric").WithGenericArguments(typeof(int)).WillReturn(3);
  var result = Dependency.CallsGuardGeneric<int>();
  Isolate.Verify.NonPublic.WasCalled(typeof (Dependency), "PrivateCallGuardGeneric", typeof (int));
}

VB

<TestMethod, Isolated>
Public Sub VerifyPrivateGenericMethod_WasCalled()
    Isolate.NonPublic.WhenCalled(Of Dependency)("PrivateCallGuardGeneric").WithGenericArguments(GetType(Integer)).WillReturn(3)
    Dim result = Dependency.CallsGuardGeneric(Of Integer)()
    Isolate.Verify.NonPublic.WasCalled(GetType(Dependency), "PrivateCallGuardGeneric", GetType(Integer))
End Sub