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
In the below example code, there is a class that derives from other, and the derived one's constructor calls to its base's constructor. I couldn't find a way to make the base constructor call mocked. I want the code inside the derived constructor to execute, but the call to the base constructor to be mocked. Is this possible ?

To give more explantion, in the test code below, when
Mumbo m = new Mumbo("abc");
is executed, the constructor of Jumbo() is called, even though it is not called when
Jumbo j = new Jumbo("abc");
is executed, because Jumbo's constructor is talled to be mocked. I was expecting at the former call, the Jumbo's constructore to be mocked as well.

class Jumbo
{
private int abc;
private string s;
public Jumbo(string s)
{
this.s = s;
}
}

class Mumbo : Jumbo
{
public Mumbo(string s) : base(s)
{
}
}

[TestFixture]
public class Testa
{
[Test]
public void Test()
{
MockManager.Init();
Mock jMock = MockManager.Mock(typeof (Jumbo));
Mumbo m = new Mumbo("abc");
Jumbo j = new Jumbo("abc");
}
}
asked by phazer (4k points)

5 Answers

0 votes
Hi,

If you want to mock a base constructor (or any other hidden base method) you will need to use the Mock.CallBase API.

Using your classes such a test should look like:
[Test]
public void TestMockBaseCtor()
{
   MockManager.Init();
   Mock mock = MockManager.Mock(typeof(Mumbo),Constructor.NotMocked);
   mock.CallBase.ExpectConstructor();

   Mumbo target = new Mumbo("abc");

   MockManager.Verify();
}


:!: you can look in TypeMock Guide "Hidden Methods" for more info.
answered by lior (13.2k points)
0 votes
Thanks, that works!

Just a suggestion, it may be good to include an example on hidden methods for base constructor mocking as well, for people like me :)
answered by phazer (4k points)
0 votes
Another thing I just noticed, there is no CallBase.ExpectConstructorAlways(), there is only CallBase.ExpectConstructor(). I am using version 3.7 right now.
answered by phazer (4k points)
0 votes
Hi
Thanks for the comments.
We have a section on hidden methods in our documentation here

CallBase have ExpectConstructorAlways method 8).
You actually found a bug in our online documentation :(
Please look at the documentation installed on your machine
(TypeMock-Developers Guide.chm) you can find it in IMockControl Members section.
answered by ohad (35.4k points)
0 votes
Hi
In any case I would like to suggest upgrading to the latest version
as there are many changes and bug fixes from version 3.7
answered by ohad (35.4k points)
...