Mocking Fields

Typemock Isolator supports mocking of field members. You can mock and set expectations for all types of members as long as they are defined as a class type (i.e., not as structures or any other value type). This includes mocking private, protected, public, generic types, instance and static fields (delegates and events cannot be mocked). Furthermore, in order to support true manipulation of an instance member, Typemock Isolator also provides an API for assigning values to members inside a class.

Example - Mocking a Field

Here is how you can set an expectation for a field member inside a mocked class. In this example, the expectation is set on the public field "field" of the tested class, faking the return value from the call  to "ReturnFive". Also, the field "field" is mocked(1) and the expectation for the call is set to "ReturnFive"(2).

Hide C# Code
Hide Visual Basic Code

// C#
// Field 

/// <summary>
/// Class that has another class as a public field
/// </summary>

public class ClassWithField
{
    public FieldClass field;

    private int privateField;

    public int GetPrivateValue()
    {
       
return privateField;
    }
}
/// <summary>
/// class which reside as a field.
/// </summary>

public class FieldClass
{
    /// <summary>
    /// will always return 5 (unless mocked)
    /// </summary>
    /// <returns>5</returns>

    public int ReturnFive()
    {
        return 5;
    }
}

[Test]
[VerifyMocks]
public void MockInstanceField()
{
    // mock next instance creation of ClassWithField
    Mock mock = MockManager.Mock<ClassWithField>();
    // mock the "field" field and set the expectation on it
  1 Mock mockedField = mock.MockField("field");
  2 mockedField.ExpectAndReturn("ReturnFive", 6);

    ClassWithField target = new ClassWithField();
    int actual = target.field.ReturnFive();
    // ReturnFive call is mocked and should return 6
    Assert.AreEqual(6, actual);

' Visual Basic
' Field

' Class that has another class as a public field
Public Class ClassWithField
   
Public member As FieldClass
    
Private privateMember As Integer

    
Public Function GetPrivate() As Integer
       
Return privateMember
    
End Function
End Class
 

' Class which reside as a field.
Public Class FieldClass
    ' will always return 5 (unless mocked)
    Public Function ReturnFive() As Integer
        Return 5
    End Function
End Class

<Test(), VerifyMocks()> _
Public Sub MockInstanceField()
   
' mock next instance creation of ClassWithField
    Dim mock As Mock = MockManager.Mock(GetType(ClassWithField))
    ' mock the "field" field and set the expectation on it
    
1 Dim MockedField As Mock = mock.MockField("field")
    
2 MockedField.ExpectAndReturn("ReturnFive", 6)

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

Tip: Static members can also be mocked using the CallStatic.MockField API.

Example - Assigning a Field

Here is how you can set a new value for a field member declared inside a mocked class. In this example, the field "privateField" is assigned a value of "40"(1) that will be set to it upon creation of the new object (2).

Hide C# Code
Hide Visual Basic Code

// C#
// Field 

[Test]
[VerifyMocks]
public void AssignField()
{
    // create the mock object
    Mock mock = MockManager.Mock<ClassWithField>();
 
   // if the field wont be assigned its default value will be 0
  1 mock.AssignField("privateField", 40);

    // verify that the 40 was assigned to the newly crated instance
  2 ClassWithField target = new ClassWithField();
   
int actual = target.GetPrivateValue();
    Assert.AreEqual(40, actual);
}

' Visual Basic
' Field

<Test(), VerifyMocks()> _
Public Sub AssignField()
    'create the mock object
    Dim mock As Mock = MockManager.Mock(GetType(ClassWithField))
   
' if the field wont be assigned its default value will be 0
  1 mock.AssignField("privateField", 40)
    ' verify that the replacement was assigned to the newly crated instance
  2 Dim field As New ClassWithField
   
Dim actual As Integer = field.GetPrivateValue()
    Assert.AreEqual(
40, actual)
End Sub

 

Tip: Static members can also be assigned using the CallStatic.AssignField API.

Copyright © Typemock Ltd. 2004-2008. All Rights Reserved.