Verifying Method Calls

To verify that a method was called, use Isolate.Verify.<Verification_Statement>.

When to Use

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

Syntax

C#

Isolate.Verify<Verification_Statement>(() => fakeDependancy.<method>);

VB

Isolate.Verify<Verification_Statement>(Function() fakeDependancy.<method>)

The following table explains the possible verification statements:

Verification Statement

Description

WasCalledWithAnyArguments()

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

WasCalledWithExactArguments()

Passes the verification, the call was made at least once with matching arguments.

WasCalledWithArguments()

Passes the verification if the call was made at least once with arguments that match the predicate

GetTimesCalled()

Returns the number of times that the method was called, which later can be asserted on.

WasNotCalled()

Passes the verification if the call was never made.

Unlike Isolate.WhenCalled() which can refer to all overloaded methods, Isolate.Verify() always checks a specific method, according to the method signature.

You can perform verification on a chain of methods. When one of the methods in the chain returns an unsupported type (including collections in mscorlib), Typemock Isolator throws an exception.

Samples

Sample 1: Verifying whether the Method Was Called with Any Arguments
C#

Isolate.Verify.WasCalledWithAnyArguments(() => fakeDependancy.CheckSecurity(null, null));

VB

Isolate.Verify.WasCalledWithAnyArguments(function() fakeDependancy.CheckSecurity(Nothing, Nothing))

Sample 2: Verifying that the Method Was Not Called

The following sample shows how to verify that the method was never called.

C#

Isolate.Verify.WasNotCalled(() => fakeDependancy.CallGuard().CheckSecurity(null,null));

VB

Isolate.Verify.WasNotCalled(function() fakeDependancy.CallGuard().CheckSecurity(Nothing, Nothing))

Sample 3: Verifying that the Method Was Called With Specific Arguments

The following sample shows how to verify more complex argument verification with a custom logic, using Isolate.Verify.WasCalledWithArguments() and Matching().

C#

Isolate.Verify.WasCalledWithArguments(() => fakeDependancy.CheckSecurity(null, null)).Matching(a => 
  (a[0] as string).StartsWith("type") &&
  (a[1] as string).StartsWith("rule"));

VB

Isolate.Verify.WasCalledWithArguments(Sub() fakeDependancy.CheckSecurity(Nothing, Nothing)).Matching(Function(a) 
  TryCast(a(0), String).StartsWith("type") AndAlso 
  TryCast(a(1), String).StartsWith("rule"))