FsUnit 显示 F# 类型的两个相同的预期值
当然,下面的两个测试失败了。但为什么错误消息显示两个相同的预期值?有没有办法只显示一个?
myTest2
返回预期:
{ N = 2 }>或<{N=2}> But was: <{ N = 1 }>
当使用普通的 NUnit 时,这会按预期工作(例如 Expected: But was:
)。
当然,当处理更复杂的类型时,这会变得非常糟糕。
Tests.cs:
namespace Blah
open FsUnit
open NUnit.Framework
module Tests =
type MyDU = A | B
type MyRecord = { N: int }
[<Test>]
let myTest ()=
A |> should equal B
[<Test>]
let myTest2 ()=
{ N = 1 } |> should equal { N = 2 }
项目文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Tests.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="FSharp.Core" Version="5.0.2" />
<PackageReference Include="FsUnit" Version="4.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
</ItemGroup>
</Project>
The two tests below fail, of course. But why do the error messages show two identical expected values? Is there a way of only showing one?
myTest
returns Expected: <B> or <B> But was: <A>
myTest2
returns Expected: <{ N = 2 }> or <{ N = 2 }> But was: <{ N = 1 }>
This works as expected (e.g. Expected: <B> But was: <A>
) when using plain NUnit instead.
This gets pretty nasty when working with more complicated types, of course.
Tests.cs:
namespace Blah
open FsUnit
open NUnit.Framework
module Tests =
type MyDU = A | B
type MyRecord = { N: int }
[<Test>]
let myTest ()=
A |> should equal B
[<Test>]
let myTest2 ()=
{ N = 1 } |> should equal { N = 2 }
Project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Tests.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="FSharp.Core" Version="5.0.2" />
<PackageReference Include="FsUnit" Version="4.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
</ItemGroup>
</Project>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从源代码来看,它看起来像 这就是原因:
equal
对于像记录和联合这样的“结构相等”项,使用 NUnit 约束:并且此约束将输入到错误消息中。
From looking at the source, it looks like this is the reason:
equal
for 'structural equatable' items like records and unions use the NUnit constraint:And this constraint will feed into the error message.