Assign a new value to the given field.
Namespace: TypeMock
Assembly: TypeMock (in TypeMock.dll) Version: 9.3.6.0 (9.3.6.0)
Syntax
Parameters
- fieldName
- Type: SystemString
The name of the field to be assigned - value
- Type: SystemObject
The value to assign.
Exceptions
Exception | Condition |
---|---|
TypeMockException | If field is not found or is static |
Examples
C#
[Test] [VerifyMocks] public void AssignField() { // create the new value we will assign to the field FieldClass replacement = new FieldClass(); // create the mock object Mock mock = MockManager.Mock<ClassWithField>(); // if the field wont be assigned accessing it will yield a null reference exception mock.AssignField("privateField", replacement); // verify that the replacement was assigned to the newly crated instance ClassWithField target = new ClassWithField(); int actual = target.GetPrivate().GetHashCode(); Assert.AreEqual(replacement.GetHashCode(), actual); }
//VB <Test(), VerifyMocks() > _ Public Sub AssignField() ' create the new value we will assign to the field Dim Replacement As New FieldClass 'create the mock object Dim mock As Mock = MockManager.Mock(GetType(ClassWithField)) ' if the field wont be assigned accessing it will yield a null reference exception mock.AssignField("privateField", Replacement) ' verify that the replacement was assigned to the newly crated instance Dim field As New ClassWithField Dim hashCode As Integer = field.GetPrivate().GetHashCode() Assert.AreEqual(Replacement.GetHashCode, hashCode) End Sub
See Also