MockAssignField Method

Typemock Isolator Developer Guide
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

public void AssignField(
	string fieldName,
	Object value
)

Parameters

fieldName
Type: SystemString
The name of the field to be assigned
value
Type: SystemObject
The value to assign.

Implements

IMockControlAssignField(String, Object)
Exceptions

ExceptionCondition
TypeMockExceptionIf field is not found or is static, or if the class's constructor is not mocked
Examples

Show how to assign values to private fields. this can also be done to private static fields using the AssignStaticField API.
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("privateMember", 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("privateMember", 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

Reference