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
Hi I have a question concerning type mock feature and constructors

When i mock a type first like
MockManager.Init();
Mock mock=MockManager.Mock(typeof(TestedClass));

and the initialize the type
TestedClass testedClass=new TestedClass();

the code inside the constructor is never executed??
How can i allow that code to be executed?

thnks
asked by tolisss (28.8k points)

8 Answers

0 votes
When i mock a type first like
MockManager.Init();
Mock mock=MockManager.Mock(typeof(TestedClass));

and the initialize the type
TestedClass testedClass=new TestedClass();

the code inside the constructor is never executed??
How can i allow that code to be executed?


Hi, This is simple use the following:
Mock mock=MockManager.Mock(typeof(TestedClass),false);


The second parameter is bool mockConstructors, by default it is true, but you can turn it off as shown above.


:idea: Tip: To mock the constructor but to run initializations of static fields you can do the following:
Mock mock=MockManager.Mock(typeof(TestedClass),false);
mock.ExpectAlways(".ctor");


I hope this helps
answered by richard (3.9k points)
0 votes
Tip: To mock the constructor but to run initializations of static fields you can do the following

[TestFixtureAttribute]
   public class test
   {
      [Test]
      public void MethodName()
      {
         MockManager.Init();
         Mock mock=MockManager.Mock(typeof(Class6),false);
         
         mock.ExpectAlways(".ctor");

      }
   }
   public class Class6
   {
      private int i=0;
      public Class6()
      {
         i=0;
      }

   }


No method .ctor in type MCH.Fixtures.Class6 returns void

why?
answered by tolisss (28.8k points)
0 votes

No method .ctor in type MCH.Fixtures.Class6 returns void


:oops: Woops, true,
my mistake. You cannot do that in the version that you have. I will see what can be done.
answered by scott (32k points)
0 votes
*deleted by mistake - sorry*
answered by tolisss (28.8k points)
0 votes
You can now Mock the constructor without mocking static fields using the following code:

MockManager.Init();
Mock mock=MockManager.Mock(typeof(Class6),false);
mock.Strict=true;
mock.MockConstructor();
answered by richard (3.9k points)
0 votes
based on the above post i expected this not to fail
public class Class3
   {
      public string test="test";
      public Class3()
      {
      }
   }

   [TestFixture]
   public class ClassName
   {
      [Test]
      public void MethodName()
      {
         MockManager.Init();
         Mock mock=MockManager.Mock(typeof(Class3),false);
            mock.MockConstructor();
            Class3 class3=new Class3();
         Assert.IsNotNull(class3.test);
      }
   }


is it possible to mock the constructor but allow the test field to be initialized?

thnks
answered by tolisss (28.8k points)
0 votes
Hi,
The above code will fail as what is really happening behind the scenes is that the public string test="test" is actually initialized in the constructor so the code is equvalent to:
public class Class3
{
      public string test;
      public Class3()
      {
          test="test";
      }
} 

You have mocked the constructor so test will not be initialized.
:!: Just to make things clear, the example code we showed is to enable static fields to be initialized.
answered by scott (32k points)
0 votes
Hi,
Mocking Constructors is much simpler in version 2.2 please see Mocking Constructors topic in the User Guide
answered by richard (3.9k points)
...