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
Hello,

how can I avoid that the constructor of the second class is called (without mocking second class)? Here is a sample code that does not work:

using System;
using System.Diagnostics;
using NUnit.Framework;
using TypeMock;

namespace ConstructorTests
{
   internal class TestedClass
   {
      private static SecondClass secondClass = new SecondClass();

      public TestedClass()
      {
         Trace.WriteLine("TestedClass..ctor() called!");
      }

   }

   internal class SecondClass
   {
      public SecondClass()
      {
         throw new NotImplementedException("Should not be called here!!!");
      }
   }


   /// <summary>
   /// Testing constructor.
   /// </summary>
   [TestFixture]
   public class ConstructorTest
   {
      Mock TestedClassMock = null;
      TestedClass TestedClassMocked = null;


      /// <summary>
      /// Constructor.
      /// </summary>
      public ConstructorTest()
      {
         MockManager.Init();

      }


      #region SetUp / Teardown

      /// <summary>
      /// Called before every executed test case
      /// </summary>
      [SetUp]
      public void SetUp()
      {
         this.TestedClassMock = MockManager.Mock(typeof(TestedClass));
      }

      /// <summary>
      /// Called after every executed test case
      /// </summary>
      [TearDown]
      public void Teardown()
      {
         MockManager.Verify();
      }

      #endregion SetUp / Teardown


      #region Test methods

      /// <summary>
      /// Test <c>OnBeforeShow</c>, should return true in Enabled
      /// </summary>
      [Test]
      public void MyTest()
      {
         this.TestedClassMocked = new TestedClass();
      }
   
      #endregion Test methods

   }
}

When you run that code the exception is thrown :cry: . How to avoid this? BTW, the second parameter in method Mock even does not help.

Thanks
Michael
asked by mknaup (8.5k points)

4 Answers

0 votes
Hi Michael,
The case that you showed is because of how the C# compiles the code, it will call the static constructor of TestedClass when the ConstuctorTest is initialized. This is before TypeMock has mocked the class, so it won't be mocked.
When you run that code the exception is thrown :cry: . How to avoid this?

:idea: To avoid this put the tested class in its own file or even better in its own assembly (which is the normal case for tests).
answered by richard (3.9k points)
0 votes
Hi Richard,

the given code was just a sample to simplify the question. In "reality" both classes, tested and second class are in a seperate assembly (but the same). The effect nevertheless was the same, the constructor of second class which needs some services not available at testing time is called. I solved the problem by mocking the second class as well, so I've a workaround for me.
May the reason for the problem in the above code be the same as for "real life" code as well? I don't know how C# calls static constructors. Perhaps there are other constellations that lead to the same problem even if the code is in a different assembly (and the two classes in different C# files)?

Thanks again for your quick and adjuvant reply,
Michael
answered by mknaup (8.5k points)
0 votes
Hi Michael,

The effect nevertheless was the same, the constructor of second class which needs some services not available at testing time is called. I solved the problem by mocking the second class as well, so I've a workaround for me.


You are quite right, This is a BUG :twisted: , it will be fixed in next release.
Thanks for pointing this out
answered by richard (3.9k points)
0 votes
Hi,
This BUG in mocking static constructors is fixed in version 2.3
answered by scott (32k points)
...