chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
:? Is it possible to mock base class Constructors? (I have tried searching the forums but come up blank)

e.g.
public class MyDerivedClass : MyBaseClass
{
    public MyDerivedClass() : base()
    {
        ....
    }
}

or
public class MyDerivedClass : MyBaseClass
{
    public MyDerivedClass(string foo) : base(foo)
    {
        ....
    }
}


:?: How do I mock the base() or base(foo) constructor calls (or should that be if!??) in my unit test for MyDerivedClass?
asked by c0d3-m0nk3y (8.7k points)

2 Answers

0 votes
Hi

This feature currently is not available.
However we added that to our feature list.

Note that TypeMock only mocks object of the mocked type
You can find here detailed explanation:
Derived Classes and Overloaded Methods behaviour
answered by ohad (35.4k points)
0 votes
Hi,
Here is a workaround of doing this.
Although you cannot tell TypeMock that the call to MyBaseClass will be mocked. You can tell TypeMock that the call to the first constuctor will run as normal and the second constructor mocked
[Test]
public void TestBase()
{
   MockManager.Init();
   Mock mock = MockManager.Mock(typeof(MyDerivedClass),Constructor.NotMocked);
   mock.ExpectUnmockedConstructor(); // call to MyDerived is made
   mock.ExpectConstructor();  // call MyBase - mocked

   MyDerivedClass d = new MyDerivedClass();


   MockManager.Verify();
}
answered by scott (32k points)
...