Faking 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 to the method under test.
Syntax
C# Isolate.Fake.Instance<Class>();
VB
Isolate.Fake.Instance(Of Class)()
Samples
C# [TestMethod, Isolated] public void FakeAConcreteObjectExample() { // Arrange - Fake a Process, default is that all Members.ReturnRecursiveFakes var fake = Isolate.Fake.Instance<Process>(); Isolate.WhenCalled(() => fake.MainModule.Site.Name).WillReturn("Typemock rocks"); // Act var result = MyCode.IsMySiteNameTypemock(fake); // Assert Assert.AreEqual(true, result); } public class MyCode { public static bool IsMySiteNameTypemock(Process process) { var name = process.MachineName; if (process.MainModule.Site.Name.StartsWith("Typemock")) return true; else return false; } }
VB
<TestMethod(), Isolated()> _
Public Sub FakeObjectExample()
' Arrange - Fake a Process, default is that all Members.ReturnRecursiveFakes
Dim fake = Isolate.Fake.Instance(Of Process)()
Isolate.WhenCalled(Function() fake.MainModule.Site.Name).WillReturn("Typemock rocks")
' Act
Dim result = MyCode.IsMySiteNameTypemock(fake)
' Assert
Assert.AreEqual(True, result)
End Sub
Public Class MyCode
Public Shared Function IsMySiteNameTypemock(process As Process) As Boolean
Dim name = process.MachineName
If process.MainModule.Site.Name.StartsWith("Typemock") Then
Return True
Else
Return False
End If
End Function
End Class