【问题标题】:xUnit test pass but getting "Catastrophic failure: System.ArgumentException"xUnit 测试通过但得到“灾难性失败:System.ArgumentException”
【发布时间】:2021-10-13 04:29:47
【问题描述】:

当使用 xunit 在 .net 5.0 项目中从命令行 (dotnet test) 运行测试时,所有测试似乎都通过了,但进程崩溃并出现以下来自 dotnet test 详细冗长的消息:

Catastrophic failure: System.ArgumentException : There is at least one object in this array that cannot be serialized (Parameter 'array')
[xUnit.net 00:00:03.74]     [FATAL ERROR] System.ArgumentException
[xUnit.net 00:00:03.74]       System.ArgumentException : There is at least one object in this array that cannot be serialized (Parameter 'array')
[xUnit.net 00:00:03.74]       Stack Trace:
[xUnit.net 00:00:03.74]         C:\Dev\xunit\xunit\src\xunit.runner.utility\Extensions\MessageSinkMessageExtensions.cs(44,0): at MessageSinkMessageExtensions.Dispatch[TMessage](IMessageSinkMessage message, HashSet`1 messageTypes, MessageHandler`1 callback)

这只是在从命令行运行 dotnet test 时发生的,从 VisualStudio 运行测试就可以了。

我正在使用 TestServer 测试 dotnet 5 rest API。

任何想法可能是什么原因?

使用的包版本:

<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="5.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">

-- 更新--

我刚刚意识到这里描述的错误是在使用 xUnit Theory 时发生的,该理论将以下示例类作为参数:

    public record TruncatedString
    {
        public TruncatedString(string value)
        {
            Value = FormatTruncatedString(value);
        }

        protected string Value { get; }

        protected static string FormatTruncatedString(string value)
        {
            return value.Substring(0,4);
        }

        public static implicit operator string(TruncatedString truncated) => truncated.Value;
        public static implicit operator TruncatedString(string text) => new (text);

        public override string ToString() => Value;
    }

并且在 xUnit 理论中使用如下:

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("ABC")]
public async Task ThestWithTheErrorMessage(TruncatedString value)
{
    // ...
    // THIS TEST PRODUCE THE SERIALIZATION ERROR
    // System.ArgumentException : There is at least one object in this array that cannot be serialized (Parameter 'array')
    //   Stack Trace:
    //     C:\Dev\xunit\xunit\src\xunit.runner.utility\Extensions\MessageSinkMessageExtensions.cs(39,0): at MessageSinkMessageExtensions.Dispatch[TMessage](IMessageSinkMessage message, HashSet`1 messageTypes, MessageHandler`1 callback)
}

【问题讨论】:

    标签: .net-core xunit dotnet-cli


    【解决方案1】:

    我遇到了同样的问题。如果发生隐式转换,xUnit 似乎不会运行理论测试。

    将您的测试重写为:

    [Theory]
    [InlineData(null)]
    [InlineData("")]
    [InlineData("ABC")]
    public async Task Test(string input)
    {
        TruncatedString value = input;
        // ...
    }
    

    xUnit 中的这个错误记录在这里:https://github.com/xunit/xunit/issues/1742

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-17
      • 2020-03-23
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多