Faking Private Objects
Using the Mocking API, you can create fake objects of interfaces as well as concrete and sealed classes.
When to Use
When you want to pass a fake object with not visible type to the method under test.
Syntax
C# Isolate.Fake.Instance(type);
VB
Isolate.Fake.Instance(type)
Resulting Fake in the test will have type 'object'. For setting behavior on it use Isolate.NonPublic API or by casting to visible base class if this is permissible.
Samples
C# [TestMethod] public void FakingInstanceOfNotVisibleType() { //Arrange var internalType = typeof(VisibleType).Assembly.GetType("ThirdPartyProject.NotVisibleTypeToFake"); var dependency = Isolate.NonPublic.Fake.Instance(internalType); var instance = new VisibleType(); //Act var result = (double)Isolate.Invoke.Method(instance, "Calculate", 0.1, 0.2, dependency); //Assert Assert.AreEqual(0.3, result, 0.1); } public class VisibleType { private double Calculate(double a, double b, NotVisibleTypeToFake dependency) { dependency.Check(); return a + b; } } internal class NotVisibleTypeToFake { public void Check() { throw new Exception("No Entry"); } }
VB <TestMethod(), Isolated()> _ Public Sub FakingInstanceOfNotVisibleType() 'Arrange Dim internalType = GetType(VisibleType).Assembly.GetType("ThirdPartyProject.NotVisibleTypeToFake") Dim dependency = Isolate.NonPublic.Fake.AllInstances(internalType) Dim instance = New VisibleType() 'Act Dim result = CType(Isolate.Invoke.Method(instance, "Calculate", 0.1, 0.2, dependency), Double) 'Assert Assert.AreEqual(0.3, result, 0.1) End Sub Public Class VisibleType Private Function Calculate(a As Double, b As Double, dependency As NotVisibleTypeToFake) As Double dependency.Check() Return a + b End Function End Class Friend Class NotVisibleTypeToFake Public Sub Check() Throw New Exception("No Entry") End Sub End Class