Faking a Private Call From a Constructor
You can control non-public methods that are called within a constructor.
Faking Private Methods
To fake private methods, use the Isolate.NonPublic. This API uses a string name of the method that you want to fake. Typemock Isolator will find the method through reflection and arrange the new behavior. To fake an instance method, pass the instance and the name of the method.
Syntax
C# var instance = Isolate.Fake.NextInstance<ObjectToMock>(Members.CallOriginal); Isolate.NonPublic.WhenCalled(<instance>, "<methodname>").<behavior>
VB
Dim instance = Isolate.Fake.NextInstance(Of ObjectToMock)(Members.CallOriginal)
Isolate.NonPublic.WhenCalled(<instance>, "<methodname>").<behavior>
Sample:
C# [TestMethod, Isolated] public void IgnorePrivateCallFromCtor() { var dependency = Isolate.Fake.NextInstance<Dependency>(Members.CallOriginal); Isolate.NonPublic.WhenCalled(dependency, "ToUpperCase").IgnoreCall(); var res = new ClassUnderTest().Create("typemock"); Assert.AreEqual("typemock", res.Name); } public class ClassUnderTest { 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; ToUpperCase(); } private void ToUpperCase() { Name.ToUpper(); } }
VB
<TestMethod, Isolated>
Public Sub IgnorePrivateCallFromCtor()
Dim dependency = Isolate.Fake.NextInstance(Of Dependency)(Members.CallOriginal)
Isolate.NonPublic.WhenCalled(dependency, "ToUpperCase").IgnoreCall()
Dim res = New ClassUnderTest().Create("typemock")
Assert.AreEqual("typemock", res.Name)
End Sub
Public Class ClassUnderTest
Public Function Create(name As String) As Dependency
Return New Dependency(0, name)
End Function
End Class
Public Class Dependency
Public Age As Integer
Public Name As String
Public Sub New(age As Integer, name As String)
Age = age
Name = name
ToUpperCase()
End Sub
Private Sub ToUpperCase()
Name.ToUpper()
End Sub
End Class