MockMockField Method

Typemock Isolator Developer Guide
Create a Mock object and assign it to a given field.

Namespace:  TypeMock
Assembly:  TypeMock (in TypeMock.dll) Version: 9.3.6.0 (9.3.6.0)
Syntax

public IMockControl MockField(
	string fieldName
)

Parameters

fieldName
Type: SystemString
The name of the field to be mocked

Return Value

Type: IMockControl
A mock object that is assigned to the field.

Implements

IMockControlMockField(String)
Exceptions

ExceptionCondition
TypeMockExceptionIf constructor is not mocked, field is not found or field can't be mocked
Examples

Show how to mock a call on a public field. this works on private members as well
C#
[Test]
[VerifyMocks]
public void MockInstanceField()
{
    // mock next instance creation of ClassWithField 
    Mock mock = MockManager.Mock(typeof(ClassWithField));
    // mock the "member" field and set the expectation on it
    Mock mockedField = mock.MockField("member");
    mockedField.ExpectAndReturn("ReturnFive", 6);

    ClassWithField target = new ClassWithField();
    int actual = target.member.ReturnFive();
    // ReturnFive call is mocked and should return 6
    Assert.AreEqual(6, actual);
}
//VB
<Test(), VerifyMocks() > _
Public Sub MockInstanceField()
    ' mock next instance creation of ClassWithField 
    Dim mock As Mock = MockManager.Mock(GetType(ClassWithField))
    ' mock the "member" field and set the expectaion on it
    Dim MockedField As Mock = mock.MockField("member")
    MockedField.ExpectAndReturn("ReturnFive", 6)

    Dim target As New ClassWithField
    Dim actual As Integer = target.member.ReturnFive
    ' ReturnFive call is mocked and should return 6
    Assert.AreEqual(6, actual)
End Sub
See Also

Reference