FsUnit showing two identical expected values for F# types

66 Views Asked by At

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>
1

There are 1 best solutions below

3
Charles Mager On

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.

Related Questions in F#