Verifying the Number of Created Instances

You can count how many times an instance was created.

When to Use

When the number of instances created is important for the test.

Syntax

C#

Isolate.Fake.AllInstances<type>();
…
Isolate.Verify.GetTimesCalled(() => new Dependency());

VB

Isolate.Fake.AllInstances(Of Dependency)()
…
Isolate.Verify.GetTimesCalled(Function() New Dependency())

Samples

The following sample shows how to verify how many instances was created.

C#

[TestMethod, Isolated]
public void Verify_FutureInsance_WasCreated3Times()
{
  var fakeDependency = Isolate.Fake.AllInstances<Dependency>();

  var dependancy1 = DependancyFactory.Create();
  var dependancy2 = DependancyFactory.Create();
  var dependancy3 = DependancyFactory.Create();

  var count = Isolate.Verify.GetTimesCalled(() => new Dependency());
  Assert.AreEqual(3, count);
}

VB

<TestMethod, Isolated>
Public Sub Verify_FutureInsance_WasCreated3Times()
    Dim fakeDependency = Isolate.Fake.AllInstances(Of Dependency)()

    Dim dependancy1 = DependancyFactory.Create()
    Dim dependancy2 = DependancyFactory.Create()
    Dim dependancy3 = DependancyFactory.Create()

    Dim count = Isolate.Verify.GetTimesCalled(Function() New Dependency())
    Assert.AreEqual(3, count)
End Sub