【发布时间】:2019-11-25 16:27:05
【问题描述】:
我有一个针对 .net 4.6.1 项目运行的 netcoreapp2.0 测试项目。安装 3.0 SDK 后,我开始在该项目的所有测试中引发 NotSupportedException。在只有旧版 SDK 或在 global.json 文件中指定 2.2 SDK 的机器上,它可以工作。
我的意思是,我怎样才能找到引发异常的原因?它被扔在我不拥有的 DLL 中,我无法使用 sourcelink。 VS 不显示任何堆栈跟踪,即使为每个异常都设置了 CLR 异常,它也不会停止异常。
这是我通过运行测试得到的唯一结果。
PS (...)> dotnet test
Test run for (...).Tests.dll(.NETCoreApp,Version=v2.2)
Microsoft (R) Test Execution Command Line Tool Version 16.3.0
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:01.35] (...) [FAIL]
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
[xUnit.net 00:00:01.43] (...) [FAIL]
[xUnit.net 00:00:01.43] (...) [FAIL]
[xUnit.net 00:00:01.43] (...) [FAIL]
[xUnit.net 00:00:01.43] (...) [FAIL]
[xUnit.net 00:00:01.43] (...) [FAIL]
[xUnit.net 00:00:01.43] (...) [FAIL]
[xUnit.net 00:00:01.43] (...) [FAIL]
[xUnit.net 00:00:01.43] (...) [FAIL]
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
X (...) [1ms]
Error Message:
System.NotSupportedException : Specified method is not supported.
(REPEATS FOR ALL THE TESTS...)
Test Run Failed.
Total tests: 30
Failed: 30
Total time: 3,2953 Seconds
更新: 我不知道在将 VS 更新到 16.3.9 (我在 16.3.8 上)和/或重新启动机器后如何或为什么它不再发生。所有测试现在都可以正常工作,无需对源进行任何修改。
git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
Test run for (...).Tests.dll(.NETCoreApp,Version=v2.0)
Microsoft (R) Test Execution Command Line Tool Version 16.3.0
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Test Run Successful.
Total tests: 431
Passed: 431
Total time: 10,5425 Seconds
虽然我无法在完全相同的场景中重现错误,但我可以故意收到相同的错误消息,并且可能是相关的。
我们所有的测试都用Xunit.TheoryAttribute、几个Xunit.TraitAttribute和许多JsonDataSourceAttribute装饰。最后一个是内部开发的,它从 JSON 文件创建一个测试场景,非常像 DataSourceAttribute 所做的(就像在 How to: Create a data-driven unit test 上解释的那样)。
即使激活“抛出时中断”并在测试方法和构造函数上添加断点,当找不到测试场景文件时,我也会收到相同的错误消息并且没有提供堆栈跟踪。
TestClassName
Source: (...)Test.cs line 35
Duration: 1 ms
Message:
System.NotSupportedException : Specified method is not supported.
也许之前的问题与未复制到输出的文件有关,但我仍然不知道为什么我没有得到堆栈跟踪或更具体的与未找到文件相关的错误。我将检查属性源并验证它是否可以改进,以及是否以某种方式隐藏了问题。
【问题讨论】:
-
调试的时候可以停在抛出异常的位置。
-
它只在断言上停止(可能是因为 IEnumerable 计算),我没有得到堆栈跟踪,只有这条消息。
-
您是否尝试过将其包装在 try-catch 中并在调试模式下观察抛出的异常?
-
奇怪的是调试器不会中断抛出的异常,即使它设置为中断,如你所说。会不会是在另一个进程中抛出了异常,而被调试的进程只是收到了某种关于它的通知?可能是您没有正确设置调试器在抛出异常时中断(在 Ctrl+Alt+E 窗口中)?
-
顺便说一句,您还必须在“工具”>“选项”>“调试”>“常规”中取消选中“仅启用我的代码”。
标签: c# .net-core .net-core-3.0