Handling Base Methods

Changing Behavior

Handling ‘hidden’ base methods is tricky when testing components.
A ‘hidden’ base is a virtual method that has an overridden implementation.
The OnBase() API is there to help in these situations.

For example:

C#

public class BaseClass
{
  public virtual int VirtualMethod(bool call)
  {
    // do something with the network
    ...
    return theValue;	
  }
}

public class DerivedClass : BaseClass
{
  public override int VirtualMethod(bool call)
  {
     return base.VirtualMethod(call) + 1;
  }
}

VB

Public Class BaseClass
    Public Overridable Function VirtualMethod([call] As Boolean) As Integer
        ' do something with the network
        ...
        Return theValue
    End Function
End Class

Public Class DerivedClass
    Inherits BaseClass
    Public Overrides Function VirtualMethod([call] As Boolean) As Integer
        Return MyBase.VirtualMethod([call]) + 1
    End Function
End Class

Now use OnBase to tell Isolator to fake only the base of the method.

C#

[TestMethod, Isolated]
public void FakeBase_OnlyBaseIsFaked()
{
  var unfake = new DerivedClass();
  Isolate.WhenCalled(()=>unfake.VirtualMethod(false)).OnBase().WillReturn(10);

  Assert.AreEqual(11, unfake.VirtualMethod(true));
}

VB

<TestMethod, Isolated>
Public Sub FakeBase_OnlyBaseIsFaked()
    Dim unfake = New DerivedClass()
    Isolate.WhenCalled(Function() unfake.VirtualMethod(False)).OnBase().WillReturn(10)

    Assert.AreEqual(11, unfake.VirtualMethod(True))
End Sub

Isolator also supports faking a base of a base, to do this simply pass the type of the base you want to fake. E.g.

C#

Isolate.WhenCalled(()=>unfake.VirtualMethod(false)).OnBase(typeof(BaseClass)).WillReturn(10);

VB

Isolate.WhenCalled(Function() unfake.VirtualMethod(False)).OnBase(GetType(BaseClass)).WillReturn(10)

In Insights you can see that the derived is not faked and the base is:


Verifying Called

To verify that a base method was called, we have added a new API OnBase().

For example:

C#

public class BaseClass
{
  public virtual int VirtualMethod(bool call)
  {
     return 1;
  }
}

public class DerivedClass : BaseClass
{
  public override int VirtualMethod(bool call)
  {
    if (call)
       return base.VirtualMethod(call) + 1;

    return 1;
  }
}

VB

Public Class BaseClass
    Public Overridable Function VirtualMethod([call] As Boolean) As Integer
        Return 1
    End Function
End Class

Public Class DerivedClass
    Inherits BaseClass
    Public Overrides Function VirtualMethod([call] As Boolean) As Integer
        If [call] Then
            Return MyBase.VirtualMethod([call]) + 1
        End If

        Return 1
    End Function
End Class

Now use OnBase to tell Isolator to verify only the base of the method.

C#

[TestMethod, Isolated]
public void VerifyBase_BaseWasNotCalled()
{
    // Same as new DerivedClass, but Isolator tracks calls
    var unfake = Isolate.Fake.Instance<DerivedClass>(Members.CallOriginal);                       
    unfake.VirtualMethod(false);
 
    // Verify that base was not called
    Isolate.Verify.OnBase().WasNotCalled(() => unfake.VirtualMethod(true));
}

VB

 <TestMethod, Isolated>
Public Sub VerifyBase_BaseWasNotCalled()
    ' Same as new DerivedClass, but Isolator tracks calls
    Dim unfake = Isolate.Fake.Instance(Of DerivedClass)(Members.CallOriginal)
    unfake.VirtualMethod(False)

    ' Verify that base was not called
    Isolate.Verify.OnBase().WasNotCalled(Function() unfake.VirtualMethod(True))
End Sub

Isolator also supports faking a base of a base, to do this simply pass the type of the base you want to fake. E.g.

C#

Isolate.WhenCalled(()=>unfake.VirtualMethod(false)).OnBase(typeof(BaseClass)).WillReturn(10);

VB

Isolate.WhenCalled(Function() unfake.VirtualMethod(False)).OnBase(GetType(BaseClass)).WillReturn(10)

OnBase is supported for NonPublic method too.