0

Has anyone experienced with the below unit test execution error both in Visual Studio IDE 2019 (16.9.3) and command line through vstest.console.exe (16.9.1)?

This happens even after installing the latest .NET 5.0.5 (5.0.202 SDK) following this only related post.

I even tried to debug into the Newtonsoft 13.0.1 but only to find the constructor just quits and throws the exception here. It does not even hit the JContainer ctor here.

The same logic runs fine during runtime but does not through/within a test [Fact] function. I extracted it out to make sure it is isolated and does not go through any mock/fakes. Seems like something with the .NET test engine somewhere?

The failing unit test project is using the following:

  • xunit (2.4.1)
  • xunit.runner.visualstudio (2.4.3)
  • NSubstitute (4.2.2)
  • mstest.testframework (2.2.3)
  • Mircrosoft.NET.Test.Sdk (16.9.4)
  • Microsoft.QualityTools.Testing.Fakes (16.7.4-beta.20330.2)

It is a .NET FW 4.6.2 targeting project with csproj file setup with .NET SDK style instead of the old .NET FW structure.

Stack trace from the unit test command line execution:

Error Message:
   System.InvalidProgramException : Common Language Runtime detected an invalid program.
  
Stack Trace:
   at Newtonsoft.Json.Linq.JContainer..ctor(JContainer other)
   at Newtonsoft.Json.Linq.JObject..ctor(JObject other)
   at Newtonsoft.Json.Linq.JObject.CloneToken()
   at Newtonsoft.Json.Linq.JContainer.EnsureParentToken(JToken item, Boolean skipParentCheck)
   at Newtonsoft.Json.Linq.JContainer.InsertItem(Int32 index, JToken item, Boolean skipParentCheck)
   at Newtonsoft.Json.Linq.JContainer.TryAddInternal(Int32 index, Object content, Boolean skipParentCheck)
   at Newtonsoft.Json.Linq.JContainer.Add(Object content)
   at Newtonsoft.Json.Linq.JArray.Add(JToken item)
   at at MyUnitTests.<MyTest_HasNeededData_ReturnsData>d__45.MoveNext() in XXXXXXXXXXXXXXXXXXXXXXX:line 643
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Please help!!! Thanks in advance.

QHQuach
  • 11
  • 3
  • What framework version do you use? You mentioned net framework 4.6.2 net core 2.1 and net 5. There are all different things with breaking changes. Firstly there was next framework with versions up to 4.8, net there were net core 1 2 and 3 and at the end there is Net 5 – zolty13 Apr 09 '21 at 00:40
  • To keep thing simple, the unit test is within a .NET Framework 4.6.2 project (but csproj is setup with .NET SDK style). I mentioned .NET 5 was because I upgrade my VS 2019 to 16.9.3 which seems to have .NET 5.0.201 SDK included. And basing on the post I found from MS Github, I upgraded to .NET 5.0.202 SDK just to see if that helps. – QHQuach Apr 09 '21 at 00:50
  • What it means sdk style – zolty13 Apr 09 '21 at 07:37
  • https://learn.microsoft.com/en-us/dotnet/core/project-sdk/overview#project-files – QHQuach Apr 09 '21 at 11:28

1 Answers1

0

After testing with a brand new test project, it turns out the issue was because of the inclusion/usage of a MS Fakes-generated assembly of the Newtonsoft.Json (seems to be regardless of version e.g. Newtonsoft.Json.13.0.0.Fakes.dll)

I was relying on this fake to intercept some of the Newtonsoft extension methods for a few test cases. At this time, I am able to remove the usage/dependency on this fake assembly so the issue can go away for now.

Not sure why it would happen or the exact root cause. I have all tests executed in a single threaded across multiple classes (through xUnit collection grouping) and MS Fakes supposes to be thread isolated so don't know what the deal is. Maybe I have to report this to both Microsoft and Newtonsoft to see if any of them would take on the task for a fix.

QHQuach
  • 11
  • 3