簡體   English   中英

如何找到 NotSupportedException 的根本原因?

[英]How to find the NotSupportedException root cause?

我有一個針對 .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所做的(如如何:創建數據驅動的單元測試中解釋的)。 即使激活“拋出時中斷”並在測試方法和構造函數上添加斷點,當找不到測試場景文件時,我也會收到相同的錯誤消息並且沒有提供堆棧跟蹤。

TestClassName
   Source: (...)Test.cs line 35
   Duration: 1 ms

  Message: 
    System.NotSupportedException : Specified method is not supported.

可能之前的問題與未復制到 output 的文件有關,但我仍然不知道為什么我沒有收到堆棧跟蹤或更具體的與未找到文件相關的錯誤。 我將檢查屬性源並驗證它是否可以改進,以及它是否以某種方式隱藏了問題。

通過將JsonDataSourceAttribute源添加到同一個解決方案中,我可以讓它停在它應該停在的地方(在這種情況下,在“找不到文件”上)。

這是原始來源。 if (.File.Exists(inputfile)) ,但Xunit.Sdk.DataAttribute隱藏它。

    public sealed class JsonDataSourceAttribute : DataAttribute
    {
        public string InputFilePath { get; }

        /// <summary>
        /// Retrieves a collection of context data
        /// </summary>
        /// <param name="inputFilePath">Json source file path</param>
        public JsonDataSourceAttribute(string inputFilePath)
        {
            this.InputFilePath = inputFilePath;
        }

        public override IEnumerable<object[]> GetData(MethodInfo testMethod)
        {
            if (testMethod == null) { throw new ArgumentNullException(nameof(testMethod)); }

            var inputfile = Path.IsPathRooted(this.InputFilePath)
                ? this.InputFilePath
                : Path.Combine(Directory.GetCurrentDirectory(), this.InputFilePath);

            if (!File.Exists(inputfile))
            {
                throw new ArgumentException($"Input file not found at '{inputfile}'.");
            }

            return this.LoadFileData(inputfile, this.EnumMap);
        }

        (...)
    }

通過將文件存在檢查移至構造函數,我能夠得到我想要的。

        public JsonDataSourceAttribute(string inputFilePath)
        {
            var inputfile = Path.IsPathRooted(inputFilePath)
                ? inputFilePath
                : Path.Combine(Directory.GetCurrentDirectory(), inputFilePath);

            if (!File.Exists(inputfile))
            {
                throw new ArgumentException($"Input file not found at '{inputfile}'.");
            }

            this.InputFilePath = inputfile;
        }

這不是“找到根本原因”的答案,但我想這是不可能的,因為Xunit.Sdk.DataAttribute上省略了原始異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM