Retrieving future faked instances

You can retrieve references of future faked objects

When to Use

When need a reference to a specific future fake.

Syntax

C#

var handle = Isolate.Fake.AllInstances<type>();
// create instances of type
var instance = Isolate.Verify.GetInstancesOf(handle)[indexOfInstance];

VB

dim handle = Isolate.Fake.AllInstances(Of <type>)()
'  create instances of type
dim instance = Isolate.Verify.GetInstancesOf(handle)(indexOfInstance)

Samples

The following sample shows how to assert the state of the second instance created.

C#
[TestMethod, Isolated]
public void Fake_MultipleFutureInstances_CheckSecondInstanceId()
{
  var handle = Isolate.Fake.AllInstances<UniqRefId>(Members.CallOriginal, ConstructorWillBe.Called);
  UniqRefId.Create2Instances();
  var instance2 = Isolate.Verify.GetInstancesOf(handle)[1];
  Assert.AreEqual(2, instance2.id);
}

//Under Test
public class UniqRefId
{
  public static int Counter = 0;
  public int id;

  public UniqRefId()
  {
    id = ++Counter;
  }

  public static void Create2Instances();
  {
    new UniqRefId();
    new UniqRefId();
  }
}

VB

<TestMethod, Isolated>
Public Sub Fake_MultipleFutureInstances_CheckSecondInstanceId()
    Dim handle = Isolate.Fake.AllInstances(Of UniqRefId)(Members.CallOriginal, ConstructorWillBe.Called)
    UniqRefId.Create2Instances()
    Dim instance2 = Isolate.Verify.GetInstancesOf(handle)(1)
    Assert.AreEqual(2, instance2.id)
End Sub

'Under Test
Public Class UniqRefId
    Public Shared Counter As Integer = 0
    Public id As Integer

    Public Sub New()
        id = System.Threading.Interlocked.Increment(Counter)
    End Sub

    Public Shared Sub Create2Instances()
        Dim one = New UniqRefId()
        Dim two = New UniqRefId()
    End Sub
End Class