【问题标题】:Why might xunit.net say "Specified method is not supported"为什么 xunit.net 会说“不支持指定的方法”
【发布时间】:2022-10-05 00:14:05
【问题描述】:

当我尝试运行我的 xUnit.net 测试时,我收到此错误:

[xUnit.net 00:00:00.63]     xunit.UnitTest1.TestTheAnswer [FAIL]
  Failed xunit.UnitTest1.TestTheAnswer [1 ms]
  Error Message:
   System.NotSupportedException : Specified method is not supported.

什么可能导致此错误?

【问题讨论】:

    标签: xunit.net


    【解决方案1】:

    在我的情况下,我在定义测试数据时缺少 static 关键字:

    public static IEnumerable<object[]> AdminDevicesFetchActionTestData() {...}
     
    [Theory]
    [MemberData(nameof(AdminDevicesFetchActionTestData))]
    public async Task HandleAsync_AdminDevicesFetchAction_ShouldReturnValidResponse(AdminDevicesFetchAction action,
        AdminDevicesResponse expectedResponse)
    {
    

    【讨论】:

      【解决方案2】:

      就我而言,我在 MemberData 属性参数中有错字。这是一个例子:

      public static object[][] MyTestIntegers =>
          new[] { 0, 1, 42, 9999999 }
              .Select(i => new object[] { i })
              .ToArray();
      
      [Theory]
      [MemberData("MyTestIntgers")]
      public void TestTheAnswer(int i) =>
          Assert.NotEqual(42, i);
      

      [MemberData("MyTestIntgers")] 应该是 [MemberData("MyTestIntegers")]

      【讨论】:

      • 你应该能够使用nameof 来回避那个......
      【解决方案3】:

      TL;博士:public


      所以,这个错误的另一个原因是属性/字段/方法 必须是public,这可能并不明显。

      弹出帮助仅提及成员必须为static,并且返回值必须与IEnumerable&lt;object[]&gt; 兼容。它没有说明访问修饰符。如问题中所述,运行时的错误消息在这方面也没有帮助。

      (我在 xunit 版本 2.4.1 上并尝试使用 private 属性,但失败了。通过使属性 public 解决。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多