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
I've had trouble deciding whether to post this under the "Bugs" or "General Testing" forums. Hopefully it's not a bug...

We have an NUnit test that uses a mocked out IDataReader interface. This test passes when run by itself, but it fails when run as part of a large suite of NUnit tests.

From the TypeMock tracer application we've deduced that the constructor for our mocked out interface is called when running this test by itself, but is not called when running the test with the rest of the tests.

The test code is shown below:

using System;
using System.Data;
using CPR.App.NMTools.TPG;
using NUnit.Framework;
using TypeMock;

namespace NMTools.TPG.Test
{
   [TestFixture]
   public class KindColorMapperTest
   {
      [TestFixtureSetUp]
      public void RunBeforeAllTest()
      {
         MockManager.Init();
      }

      [TearDown]
      public void Teardown()
      {
         MockManager.Verify();
      }
      
      [Test, Category("Fast")]
      public void GetWithNoDataReturned()
      {
         KindColorMapper kindColorMapper = new KindColorMapper();
         MockObject mockDataReaderDm = MockManager.MockObject(typeof (IDataReader));
         mockDataReaderDm.Strict = true;

         mockDataReaderDm.ExpectAndReturn ("Read", false);
         mockDataReaderDm.ExpectCall("Dispose");
         
         KindColor[] KindColors = kindColorMapper.GetKindColors(mockDataReaderDm.Object as IDataReader);
         
         Assert.AreEqual(0, KindColors.Length, "Train Kinds length invalid");
      }
   }
}


I just realized I can't attach a picture of our TypeMock.NET trace output. I'll try to describe it. When we run the test class by itself, our trace says:
Initialization 0|MockIDataReader|Instance 0|<Constructor> (1)...

Which is what we expect. When we run it as part of our test suite, we get:
Initialization 185|MockIDataReader|Instance 0|<Constructor> (0)...

We've ensured all our test classes that use TypeMock use MockManager.Init(); in the NUnit TestFixtureSetUp and MockManager.Verify(); in the NUnit TearDown. We are using the free version of TypeMock 3.6.0.0 . We have other tests that mock out interfaces using TypeMock without this problem.

If anybody has any suggestions on what we can do to get this test to pass that'd be great.

Thanks,
Jared
asked by jared_hagel (600 points)

1 Answer

0 votes
Hi Jared
Welcome to the forum 8)
Few points:
1. You can save the Tracer output (File menu -> Save) and send it to me. I will send you a mail with my address offline.
2. Can you please post the exact error message you received?

:idea: Tip
It is a better pattern to use MockManager.ClearAll() method in the tear down method and MockManager.Verify() in the test itself. The reason is that if a test fail before the call MockManager.Verify you still have all your expectations cleared.
answered by ohad (35.4k points)
...