Faking All Instances of a Type

Faking all instances of the same type creates a handle, on which the desired behavior of all past and future instances of that type can be set.

Faking all instances also fakes instances that were previously created, even before the test ran (for example, in the test class setup method).

You can use this capability to fake singletons.

If no behavior is explicitly set, faking all instances sets the default behavior for all past and future live objects.

Instances faked with Isolate.Fake.Instance() or Isolate.Fake.NextInstance() will not be affected by behaviors set on all instances and are handled separately.

When to Use

When you want to fake all objects of a specific type.

Syntax

C#

Isolate.Fake.AllInstances<Dependency>();

VB

Isolate.Fake.AllInstances(Of Dependency)()

By default, constructors are faked on future objects. To change this behavior, use the overloaded versions of Isolate.Fake.Instance() or Isolate.Fake.AllInstances().

Samples

Sample 1: Faking All Instances of the Same Type

The following sample shows how to fake all instances of the same type.

C#

[TestMethod, Isolated]
public void Fake_AllFutureInstances()
{
  var handle = Isolate.Fake.AllInstances<Dependency>();
 
  var result = ClassUnderTest.AddHeavlySecured(1, 2);
 
  Assert.AreEqual(3, result);
}

public class ClassUnderTest
{
  public static int AddHeavlySecured(int x, int y)
  {
    var dependency = new Dependency();
    dependency.Check();

    //run again!
    var dependency2 = new Dependency();
    dependency2.Check();

    return x + y;
  } 
}

public class Dependency
{
  public void Check()
  {
    throw new Exception("No Entry");
  }
}

VB

<TestMethod(), Isolated()> _
Public Sub Fake_AllFutureInstances()
    
    Dim handle = Isolate.Fake.AllInstances(Of Dependency)()
 
    Dim result = ClassUnderTest.AddHeavlySecured(1, 2)
 
    Assert.AreEqual(3, result)
End Sub

Public Class ClassUnderTest
  Public Shared Function AddHeavlySecured(x As Integer, y As Integer) As Integer
    Dim dependency = New Dependency()
    dependency.Check()

    ' run again!
    Dim dependency2 = New Dependency()
    dependency2.Check()

    Return x + y
  End Function
End Class

Public Class Dependency
  Public Sub Check()
    Throw New Exception("No Entry")
  End Sub
End Class

Sample 2: Faking Singletons

The following same shows how to replace the singleton object with a fake object that has a specific behavior.

Isolate.Fake.NextInstance() fakes has precedence over Isolate.Fake.AllInstances().

C#

[TestMethod, Isolated]
public void FakeSingletonExample()
{
  // Here we are setting the same behavior on all instances.
  // The behavior we set on fake will apply to past instance as well
  var fakeSingleton = Isolate.Fake.AllInstances<Singleton>();
  Isolate.WhenCalled(() => fakeSingleton.ReturnZero()).WillReturn(10);

  // Assert that the behavior applied to all instances.
  Assert.AreEqual(10, Singleton.Instance.ReturnZero());
}

public class Singleton
{
  private Singleton() { }
  static readonly Singleton instance = new Singleton();

  public static Singleton Instance { get { return instance; } }

  public int ReturnZero()
  {
    return 0;
  }
}

VB

<TestMethod(), Isolated()> _
Public Sub FakeSingletonExample()
  
  ' Here we are setting the same behavior on all instances.
  ' The behavior we set on fake will apply to past instance as well
  Dim fakeSingleton = Isolate.Fake.AllInstances(Of Singleton)()
  Isolate.WhenCalled(Function() fakeSingleton.ReturnZero()).WillReturn(10)

  ' Assert that the behavior works.
  Assert.AreEqual(10, Singleton.Instance.ReturnZero())
End Sub
 
Public Class Singleton 
  Private Sub New()
  End Sub
 
  Shared ReadOnly m_instance As New Singleton()
 
  Public Shared ReadOnly Property Instance() As Singleton
    Get
      Return m_instance
    End Get
  End Property
 
  Public Function ReturnZero() As Integer
    Return 0
  End Function
End Class

Verifying is done on the instance returned from Isolate.Fake.NextInstance.

Use Isolate.Verify.GetInstancesOf to get the future instances, this can be used for Argument Validations and field Assertions.

See another example of Faking Singleton with HttpContext here.