Visual Studio 2019 单元测试失败并显示 InvalidProgramException

问题描述

是否有人在 Visual Studio IDE 2019 (16.9.3) 和命令行中通过 vstest.console.exe (16.9.1) 遇到以下单元测试执行错误

即使在 this only related post 之后安装最新的 .NET 5.0.5 (5.0.202 SDK) 后也会发生这种情况。

我什至尝试调试到 Newtonsoft 13.0.1,但只发现构造函数只是退出并抛出异常 here。它甚至没有击中 JContainer 构造函数 here

相同的逻辑在运行时运行良好,但不会通过/在测试 [Fact] 函数内运行。我将它提取出来以确保它是隔离的并且不会经过任何模拟/伪造。似乎在某处使用 .NET 测试引擎?

失败的单元测试项目正在使用以下内容

  • 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)

这是一个面向 .NET FW 4.6.2 的项目,其 csproj 文件设置为 .NET SDK 样式,而不是旧的 .NET FW 结构。

来自单元测试命令行执行的堆栈跟踪:

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()

请帮忙!!!提前致谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)