Ignoring Base Class Constructor

To ignore a base class constructor, use the BaseConstructor behavior of Isolate.Fake.Instance().

When to Use

You can ignore the base class constructor when you use a fake object and want to call the real constructor while ignoring the base class constructor. For example, you can use this approach when the fake object's constructor performs initializations required for the test, but the base classes have external dependencies in their construction which should be faked.

All bases' constructors higher in the hierarchy also will be ignored.

Syntax
C#

Isolate.Fake.Instance<Derived>(Members.CallOriginal, ConstructorWillBe.Called, BaseConstructorWillBe.Ignored);

VB
 
Isolate.Fake.Instance(Of Derived)(Members.CallOriginal, ConstructorWillBe.Called, BaseConstructorWillBe.Ignored)

Samples

Sample 1: Ignoring Base Constructor

The following sample shows how to create Derived object, but avoid calling the base class constructor (because otherwise, it will throw the exception), BaseConstructorWillBe.Ignored is used.

A TypeMockException will be thrown if you try to fake base constructor behavior on types which are not inherited (that is inherited from System.Object) or mscorlib.dll types which are currently not supported. See Supported Fakeable MSCorlib Types.

To fake a base class higher in the hierarchy, send the base class type to fake to Isolate.Fake.Instance(). All constructors will be called up to that type.

C#

[TestMethod, Isolated]
public void CallConstructor_FakeBaseClassConstructor()
{
  // create an instance of Derived, but avoid calling the base class constructor
  var dependency = Isolate.Fake.Instance<Derived>(Members.CallOriginal, ConstructorWillBe.Called, BaseConstructorWillBe.Ignored);

  var result = new ClassUnderTest().GetSize(dependency);

  Assert.AreEqual(100, result);
}

public class ClassUnderTest
{
  public int GetSize(Derived derived)
  {
    return derived.Size;
  }
}

public class Base
{
  public Base()
  {
    throw new NotImplementedException();
  }

  public virtual int Size { get; set; }
}
 
public class Derived : Base
{
  public Derived()
  {
    Size = 100;
  }
}

VB

<TestMethod(), Isolated()> _
Public Sub CallConstructor_FakeBaseClassConstructor()
  ' create an instance of Derived, but avoid calling the base class constructor
  Dim dependency = Isolate.Fake.Instance(Of Derived)(Members.CallOriginal, ConstructorWillBe.Called, BaseConstructorWillBe.Ignored)

  Dim result = New ClassUnderTest().GetSize(dependency)

  Assert.AreEqual(100, result)
End Sub

Public Class ClassUnderTest
  Public Function GetSize(derived As Derived) As Integer
    Return derived.Size
  End Function
End Class

Public Class Base
  Public Sub New()
    Throw New NotImplementedException()
  End Sub

  Public Property Size() As Integer
    Get
      Return m_Size
    End Get
    Set(value As Integer)
      m_Size = Value
    End Set
  End Property

  Private m_Size As Integer
End Class
 
Public Class Derived
Inherits Base
  Public Sub New()
    Size = 100
  End Sub
End Class

Sample 2: Ignoring Base Class Constructors Higher Up the Hierarchy

The following sample shows how to create Derived object, but avoid calling two higher base class constructors.

C#

public class MainBase
{
  public int mainVal;
  public MainBase(int val)
  {
    mainVal = val;
  }
}

public class SecondBase : MainBase
{
  public int secondVal;
  public SecondBase(int val) : base(val + 1)
  {
    secondVal = val;
  }
}

public class FirstBase: SecondBase
{
  public int firstVal;
  public FirstBase(int val) : base(val + 1)
  {
    firstVal = val;
  }
}

public class Derived : FirstBase
{
  public int val;
  Derived(int val) : base(val + 1)
  {
    this.val = val;
  }
}

[TestMethod, Isolated]
public void CallConstructor_IgnoreLastBaseConstructors()
{
  // create an instance of Derived, but avoid calling the SecondBase and higer class constructors.
  var fake = Isolate.Fake.Instance<Derived>(Members.CallOriginal, ConstructorWillBe.Called, BaseConstructorWillBe.Ignored, typeof(SecondBase));

  Assert.AreEqual(0, fake.val);
  Assert.AreEqual(1, fake.firstVal);
  Assert.AreEqual(0, fake.secondVal);
  Assert.AreEqual(0, fake.mainVal);
}

VB

Public Class SecondBase
    Inherits MainBase
    Public secondVal As Integer
    Public Sub New(val As Integer)
        MyBase.New(val + 1)
        secondVal = val
    End Sub
End Class

Public Class FirstBase
    Inherits SecondBase
    Public firstVal As Integer
    Public Sub New(val As Integer)
        MyBase.New(val + 1)
        firstVal = val
    End Sub
End Class

Public Class Derived
    Inherits FirstBase
    Public val As Integer
    Private Sub New(val As Integer)
        MyBase.New(val + 1)
        Me.val = val
    End Sub
End Class

<TestMethod(), Isolated()>
Public Sub CallConstructor_IgnoreLastBaseConstructors()
    ' create an instance of Derived, but avoid calling the SecondBase and higer class constructors.
    Dim fake = Isolate.Fake.Instance(Of Derived)(Members.CallOriginal, ConstructorWillBe.Called, BaseConstructorWillBe.Ignored, GetType(SecondBase))

    Assert.AreEqual(0, fake.val)
    Assert.AreEqual(1, fake.firstVal)
    Assert.AreEqual(0, fake.secondVal)
    Assert.AreEqual(0, fake.mainVal)
End Sub