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
This code:
   [TestFixture]
   public class DemonstrateError {
      [Test]
      public void TestTypeMockException() {
         TestClass mockTestClass = (TestClass)RecorderManager.CreateMockedObject(typeof(TestClass), Constructor.Mocked);

         Assert.IsTrue(true);
      }
   }
   
   public class TestClass {
      public TestClass() { }
      static TestClass() {   }
      public TestClass(string stringVar) { }
      public TestClass(System.Data.IDbConnection conn) { }

   }


causes this exception:

---------------------------
UnitTestRunner
---------------------------
System.ArgumentException: Item has already been added. Key in dictionary: 'd'  Key being added: 'd'



Server stack trace: 

   at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)

   at System.Collections.Hashtable.Add(Object key, Object value)

   at TypeMock.MockManager.a(String A_0, String A_1, Object[] A_2, Boolean[] A_3)

   at TypeMock.RecorderManager.a(String A_0, String A_1, Object[] A_2)

   at TypeMock.RecorderManager.a(String A_0, String A_1, Object A_2, Object[] A_3, MethodBase A_4, Object A_5)

   at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Object[] A_4)

   at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters)

   at JetBrains.ReSharper.UnitTestSupport.PluginInterface.UnitTestAssemblyBase.get_Listener() in c:AgentworkReSharper2.0srcUnitTestSupportsrcPluginInterfaceUnitTestAssemblyBase.cs:line 57

   at JetBrains.ReSharper.UnitTestSupport.NUnit.NUnitFixture.Run(MethodInfo[] methodsToRun, Boolean[] explicitFlags) in c:AgentworkReSharper2.0srcUnitTestSupport.NUnitsrcNUnitFixture.cs:line 325

   at JetBrains.ReSharper.UnitTestSupport.NUnit.NUnitFixture.Run(String[] methodNames, Boolean[] explicitFlags) in c:AgentworkReSharper2.0srcUnitTestSupport.NUnitsrcNUnitFixture.cs:line 249

   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)

   at System.Runtime.Remoting.Messaging.StackBuilderSink.PrivateProcessMessage(RuntimeMethodHandle md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)

   at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)



Exception rethrown at [0]: 

   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)

   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)

   at JetBrains.ReSharper.UnitTestSupport.PluginInterface.ITestFixture.Run(String[] methodNames, Boolean[] explicitFlags)

   at JetBrains.ReSharper.UnitTestSupport.Runner.TestRunner.Run(String[] args) in c:AgentworkReSharper2.0srcUnitTestSupportsrcTestRunnerTestRunner.cs:line 175
---------------------------
OK   
---------------------------



Running this in NUnit 2.2.8 causes NUnit to hang, I only found the exception using Resharper's Unit Test Runner.
asked by ksummerlin (4k points)

2 Answers

0 votes
The workaround is to use the reflection API for now. In my code I just created a method that hides the details.

   [TestFixture]
   public class DemonstrateError {
      [Test]
      public void TestTypeMockException() {
         TestClass mockTestClass = MockObjectWithStaticConstructorBug<TestClass>(Constructor.Mocked);
         //TestClass mockTestClass = (TestClass)RecorderManager.CreateMockedObject(typeof(TestClass), Constructor.StaticNotMocked);
         Assert.IsTrue(true);
      }

      private T MockObjectWithStaticConstructorBug<T>(Constructor mockConstructors) {
         //TypeMock.NET Bug:
         //(TDDbConnection)RecorderManager.CreateMockedObject(typeof(TDDbConnection), Constructor.Mocked);
         MockObject mockTDDbConnection = MockManager.MockObject(typeof(T), mockConstructors);
         return (T)mockTDDbConnection.Object;
      }
   }
   
   public class TestClass {
      public TestClass() { }
      static TestClass() {   }
      //public TestClass(string stringVar) { }
      //public TestClass(System.Data.IDbConnection conn) { }

   }

answered by ksummerlin (4k points)
0 votes
Will send patch offline
answered by scott (32k points)
...