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
0 votes
We have a problem when trying to debug code that uses TypeMock. I have pasted a repro case below. When we run the program below in the Visual Studio 2005 debugger it crashes with a stack overflow. If we run it outside the debugger it does not crash. We are using TypeMock 4.0.3.0 with NUnit 2.4.1.0.

A separate but possibly related problem is another crash that we can only produce with a more complicated example. The exception in these cases is:

An unhandled exception of type 'System.ApplicationException' occurred in nunit.core.dll

Additional information: Exception in pump thread


Again, this only happens when we attempt to debug the tests in Visual Studio, not when we run them outside the debugger.

Here is the easily reproduced example:

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using NUnit.Util;
using NUnit.Core;
using NUnit.Framework;
using TypeMock;

namespace TypeMockBugRepro
{
   public class Program
   {
      static void Main(String[] args)
      {
         NUnitTestRunner.RunAllTests(Assembly.GetEntryAssembly());
      }
   }

   [TestFixture]
   [ClearMocks]
   public class Tests
   {
      public int FunctionToMock(int i)
      {
         return 1;
      }

      public int Function(int i)
      {
         return FunctionToMock(i);
      }

      [Test]
      [VerifyMocks]
      public void ExampleTest()
      {
         using (RecordExpectations recorder = RecorderManager.StartRecording())
         {
            FunctionToMock(2);
            recorder.CheckArguments();
            recorder.Return(2);
         }
         Assert.AreEqual(2, Function(2));
      }

   }

   public static class NUnitTestRunner
   {
      public static void RunAllTests(Assembly assembly)
      {
         string assemblyName = assembly.Location;

         TestDomain testDomain = new TestDomain();
         NUnitProject project = NUnitProject.FromAssembly(assemblyName);

         TestPackage testPackage = project.MakeTestPackage();
         testDomain.Load(testPackage);

         FailingTestReporter listener = new FailingTestReporter();
         TestResult result = testDomain.Run(listener);
      }
   }

   public class FailingTestReporter : MarshalByRefObject, EventListener
   {
      public void RunFinished(Exception exception) { }
      public void RunFinished(TestResult result) { }
      public void RunStarted(string name, int testCount) { }
      public void SuiteFinished(TestSuiteResult result) { }
      public void SuiteStarted(TestName testName) { }
      public void TestOutput(TestOutput testOutput) { }
      public void TestStarted(TestName testName) { }
      public void UnhandledException(Exception exception) { }
      public void TestFinished(TestCaseResult result) {}
   }   
}
asked by apatrick (600 points)

1 Answer

0 votes
Hi
We have a known bug when trying to debug inside the recording block.
The bug you posted is different though.
Until we'll fix it please try the solution below:

The workaround for the problem is to debug using another runner.
You can use TestDriven.NET
This will let debug from Visual Studio
(Right click on the test method-> Test with -> Debugger)

Another option is to use one of the NUnit runners nunit-console.exe or nunit.exe
To run them with TypeMock enabled use TMockRunner.exe e.g:
<TypeMocklPath>TMockRunner.exe <NunitPath>
unit.console.exe MyTest.dll

You should place the line: System.Diagnostics.Debugger.Break();
at the start of the debugged method.
So the test method will look like this:

public void ExampleTest()
{
   System.Diagnostics.Debugger.Break();
   using (RecordExpectations recorder = RecorderManager.StartRecording())
   {
      FunctionToMock(2);
      recorder.CheckArguments();
      recorder.Return(2);
   }
   Assert.AreEqual(2, Function(2));
}

This will halt execution and let you choose a debugger.
You can than stop anywhere inside the test as long as it is outside the recording block.

Please tell me if it helps.
answered by ohad (35.4k points)
...