.net-core - xUnit 测试通过但得到“灾难性失败:System.ArgumentException”
问题描述
当使用 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
.
任何想法可能是什么原因?
使用的软件包版本:
<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 中使用:
[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)
}
解决方案
I've run into the same issue. It seems that xUnit will not run the theory test if there's an implicit conversion happening.
Rewrite your test to be like:
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("ABC")]
public async Task Test(string input)
{
TruncatedString value = input;
// ...
}
This bug in xUnit is documented here: https://github.com/xunit/xunit/issues/1742
推荐阅读
- tensorflow - 如何使用 tf 会话根据网络输出更新游戏状态以进行强化学习
- database - 如何不公开 base64 编码的 UUID
- yocto - 用 meta-openembedded 中的 ntp 替换busybox ntpd
- javascript - 如何修复或抑制误报“您可能在组件渲染函数中有无限更新循环”Vue 警告
- javascript - 如何在 Flow 中使用相同泛型类型的值对泛型函数进行类型检查
- ruby-on-rails - 记录/跟踪在其渲染序列中使用了哪些 Rails 部分和视图
- spring - Spring-Cloud-Config 自定义环境仓库
- angular - 角度模块延迟加载,路由器子级不显示组件
- java - 选择某些组合框项目时显示 JFrame 内容
- django-rest-framework - 使用 ngx-leaflet 从 Angular 中的 Django Rest Framework 创建多个点的可用 geojson