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
Parameters
- fieldName
- Type: SystemString
The name of the field to be mocked
Return Value
Type: IMockControlA mock object that is assigned to the field.
Implements
IMockControlMockField(String)Exceptions
Exception | Condition |
---|---|
TypeMockException | If constructor is not mocked, field is not found or field can't be mocked |
Examples
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