Faking Private Instance Fields
Using the Non-Public API you can control non-public fields of the object
When to Use
When you want to get or set non-public field of the object.
Syntax
C# //To get instance field var value = Isolate.NonPublic.InstanceField(instance, "PrivateField").Value; //To set instance field Isolate.NonPublic.InstanceField(instance, "PrivateField").Value = value;
VB
'To get instance field
Dim value = Isolate.NonPublic.InstanceField(instance, "PrivateField").Value
'To set instance field
Isolate.NonPublic.InstanceField(instance, "PrivateField").Value = value
By default, fields are faked in one instance of the type. Use Isolate.Fake.NextInstance() to set up the future instance of the type (See Sample 2).
Samples
Sample 1: Getting and Setting Private Instance Field Type
The following sample shows how to control private instance field.
C# public class ClassUnderTest { private string PrivateField; public ClassUnderTest() { PrivateField = "Typemock"; } } [TestMethod] public void GetPrivateField() { //Arrange var instance = new ClassUnderTest(); //Act var result = Isolate.NonPublic.InstanceField(instance, "PrivateField").Value; //Assert Assert.AreEqual("Typemock", result); } [TestMethod] public void SetPrivateField() { //Arrange var instance = new ClassUnderTest(); Isolate.NonPublic.InstanceField(instance, "PrivateField").Value = "Typemock Rocks"; //Act var result = Isolate.NonPublic.InstanceField(instance, "PrivateField").Value; //Assert Assert.AreEqual("Typemock Rocks", result); }
VB
Public Class ClassUnderTest
Private PrivateField As String
Public Sub New()
PrivateField = "Typemock"
End Sub
End Class
<TestMethod>
Public Sub GetPrivateField()
'Arrange
Dim instance = New ClassUnderTest()
'Act
Dim result = Isolate.NonPublic.InstanceField(instance, "PrivateField").Value
'Assert
Assert.AreEqual("Typemock", result)
End Sub
<TestMethod>
Public Sub SetPrivateField()
'Arrange
Dim instance = New ClassUnderTest()
Isolate.NonPublic.InstanceField(instance, "PrivateField").Value = "Typemock Rocks"
'Act
Dim result = Isolate.NonPublic.InstanceField(instance, "PrivateField").Value
'Assert
Assert.AreEqual("Typemock Rocks", result)
End Sub
Sample 2: Setting Private Field for Future Instance of The Type
The following sample shows how to handle private fields of future instance of the type
You can use only Isolate.Fake.NextInstance(). Isolate.Fake.AllInstances() doesn't work with this API.
C# public class ClassUnderTest { private string PrivateField; public ClassUnderTest() { PrivateField = "Typemock"; } } [TestMethod] public void PrivateFieldForNextInstance() { //Arrange var handle = Isolate.Fake.NextInstance<ClassUnderTest>(); var instance = new ClassUnderTest(); Isolate.NonPublic.InstanceField(instance, "PrivateField").Value = "Typemock Rocks Again"; //Act var result = Isolate.NonPublic.InstanceField(instance, "PrivateField").Value; //Assert Assert.AreEqual("Typemock Rocks Again", result); }
VB
Public Class ClassUnderTest
Private PrivateField As String
Public Sub New()
PrivateField = "Typemock"
End Sub
End Class
<TestMethod>
Public Sub PrivateFieldForNextInstance()
'Arrange
Dim handle = Isolate.Fake.NextInstance(Of ClassUnderTest)()
Dim instance = New ClassUnderTest()
Isolate.NonPublic.InstanceField(instance, "PrivateField").Value = "Typemock Rocks Again"
'Act
Dim result = Isolate.NonPublic.InstanceField(instance, "PrivateField").Value
'Assert
Assert.AreEqual("Typemock Rocks Again", result)
End Sub
Sample 3: Setting Private Field as Recursive Fake
The following sample shows how set the private field to be a recursive fake
C# public class ClassUnderTest { private string PrivateField; private ToFake toFake = null; public ClassUnderTest() { PrivateField = "Typemock"; } public int CallFoo() { return toFake.Foo(); } } [TestMethod] public void PrivateFieldForNextInstance() { //Arrange var instance = new ClassUnderTest(); var fake = Isolate.NonPublic.InstanceField(instance, "toFake").SetRecursiveFake<ToFake>(); Isolate.WhenCalled(() => fake.Foo()).WillReturn(1); //Act var result = instance.CallFoo(); //Assert Assert.AreEqual(1, result); }
VB
Public Class ClassUnderTest
Private PrivateField As String
Private toFake As ToFake = Nothing
Public Sub New()
PrivateField = "Typemock"
End Sub
Public Function CallFoo() As Integer
Return toFake.Foo()
End Function
End Class
<TestMethod>
Public Sub PrivateFieldForNextInstance()
'Arrange
Dim instance = New ClassUnderTest()
Dim fake = Isolate.NonPublic.InstanceField(instance, "toFake").SetRecursiveFake(Of ToFake)()
Isolate.WhenCalled(Function() fake.Foo()).WillReturn(1)
'Act
Dim result = instance.CallFoo()
'Assert
Assert.AreEqual(1, result)
End Sub