Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

Type mock and constructors

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 Jan 31, 2005 by tolisss (3,660 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 Jan 31, 2005 by richard (3,900 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 Feb 1, 2005 by tolisss (3,660 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 Feb 1, 2005 by scott (29,080 points)
0 votes
*deleted by mistake - sorry*
answered Feb 1, 2005 by tolisss (3,660 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 Feb 4, 2005 by richard (3,900 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 Feb 11, 2005 by tolisss (3,660 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 Feb 12, 2005 by scott (29,080 points)
0 votes
Hi,
Mocking Constructors is much simpler in version 2.2 please see Mocking Constructors topic in the User Guide
answered Mar 23, 2005 by richard (3,900 points)
...