FsUnit 显示 F# 类型的两个相同的预期值

发布于 2025-01-09 09:11:07 字数 1354 浏览 1 评论 0原文

当然,下面的两个测试失败了。但为什么错误消息显示两个相同的预期值?有没有办法只显示一个?

myTest 返回预期:但结果是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期以后 2025-01-16 09:11:08

从源代码来看,它看起来像 这就是原因

equal 对于像记录和联合这样的“结构相等”项,使用 NUnit 约束:

Is.EqualTo(x).Or.EqualTo(x).Using<'T>(Equality.Structural)

并且此约束将输入到错误消息中。

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:

Is.EqualTo(x).Or.EqualTo(x).Using<'T>(Equality.Structural)

And this constraint will feed into the error message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文