Running a Custom Logic
When to Use
When you want to run your logic while the constructor runs.
Syntax
C# var handle = Isolate.Fake.NextInstance<Dependency>(Members.ReturnRecursiveFakes, context=> { // action here });
VB
Dim handle = Isolate.Fake.NextInstance(Of Dependency)(Members.ReturnRecursiveFakes,
Function/Sub(context)
// action here
End Function/Sub)
Samples
To run custom logic in a faked method, use DoInstead after WhenCalled and provide your own delegate. DoInstead passes MethodCallContext with the following members:
Member |
Description |
Instance |
An instance of the calling object (null if the method is static) |
Parameters |
Array of objects of arguments passed to the method (if exist) |
Method |
A MethodBase object that contains the method metadata that can be used for choosing the required behavior |
WillCallOriginal() |
Calls the original implementation |
Sample 1
C# [TestMethod, Isolated] public void CallConstructor_DoCustomCode() { var handle = Isolate.Fake.NextInstance<Dependency>(Members.ReturnRecursiveFakes, context => { if ((string) context.Parameters[1] == "typemock") { // contructor is faked return; } context.WillCallOriginal(); // constructor will be called }); var classUnderTest = new ClassUnderTest(); var dependency = classUnderTest.Create("typemock"); var result = classUnderTest.GetString(dependency); Assert.AreEqual("0", result); } public class ClassUnderTest { public string GetString(Dependency user) { return user.Name + user.Age.ToString(); } public Dependency Create(string name) { return new Dependency(0, name); } } public class Dependency { public int Age; public string Name; public Dependency(int age, string name) { Age = age; Name = name; } }
VB
<TestMethod(), Isolated()>
Public Sub CallConstructor_DoCustomCode()
Dim handle = Isolate.Fake.
NextInstance(Of Dependency)(Members.ReturnRecursiveFakes, Sub(context)
If DirectCast(context.Parameters(1), String) = "typemock" Then
' contructor is faked
Return
End If
' constructor will be called
context.WillCallOriginal()
End Sub)
Dim classUnderTest = New ClassUnderTest()
Dim dependency = classUnderTest.Create("typemock")
Dim result = classUnderTest.GetString(dependency)
Assert.AreEqual("0", result)
End Sub
Sample 2: Setting a Field from a Constructor
C# [TestMethod,Isolated] public void SetNewDependencyName_NameWillBeTypemockRocks() { Isolate.Fake.NextInstance<Dependency>(Members.ReturnRecursiveFakes,context=> { (context.Instance as Dependency).Name = "Typemock Rocks!!"; }); var result = new ClassUnderTest().Create(); Assert.AreEqual("Typemock Rocks!!", result.Name); } public class ClassUnderTest { private Dependency dependency; public Dependency Create() { dependency = new Dependency(); return dependency; } } public class Dependency { public string Name { get; set; } public Dependency() { Name = "Typemock"; } }
VB
<TestMethod(), Isolated()>
Public Sub SetNewDependencyName_NameWillBeTypemockRocks()
Isolate.Fake.NextInstance(Of Dependency)(Members.ReturnRecursiveFakes, Function(context)
TryCast(context.Instance, Dependency).Name = "Typemock Rocks!!"
End Function)
Dim result = New ClassUnderTest().Create()
Assert.AreEqual("Typemock Rocks!!", result.Name)
End Sub
Public Class ClassUnderTest
Private dependency As Dependency
Public Function Create() As Dependency
dependency = New Dependency()
Return dependency
End Function
End Class
Public Class Dependency
Public Property Name() As String
Get
Return m_Name
End Get
Set
m_Name = Value
End Set
End Property
Private m_Name As String
Public Sub New()
Name = "Typemock"
End Sub
End Class