fsprojects / FsUnit

FsUnit makes unit-testing with F# more enjoyable. It adds a special syntax to your favorite .NET testing framework.

Home Page:http://fsprojects.github.io/FsUnit/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ofCase doesn't support generic union type

wenLiangcan opened this issue · comments

Description

Checking case of generic union type throws System.InvalidCastException.

Repro steps

type MyCase<'T> =
    | Case1 of 'T
    | Case2

let intCase1 = Case1 1
intCase1 |> should be (ofCase <@ Case1 @>)

Expected behavior

The testing should be passed.

Actual behavior

The following exception is throwed.

  Failed Test  [49 ms]
  Error Message:
   System.InvalidCastException : Unable to cast object of type 'Case1[System.Int32]' to type 'MyCase`1[System.Object]'.
  Stack Trace:
     at lambda_method34(Closure , Object )
   at FsUnit.Common.isOfCase@50.Invoke(a x)
   at FsUnit.CustomConstraints.OfSameCaseConstraint.ApplyTo[TActual](TActual actual)
   at NUnit.Framework.Assert.That[TActual](TActual actual, IResolveConstraint expression, String message, Object[] args)
   at NUnit.Framework.Assert.That[TActual](TActual actual, IResolveConstraint expression)
   at FsUnit.TopLevelOperators.should[a,a](FSharpFunc`2 f, a x, Object y)

Known workarounds

None.

Related information

  • Windows 11
  • FsUnit 4.0.6
  • .NET 5

that is how type inference works in F#.
Compiler has no information to identify type of Case1 inside quotation, that is why it uses most generic one MyCase

image

in this case you have to manually specify type to help compiler

image

otherwise types are different and they should not be equal

@sergey-tihon Thanks, did not know this way to specify the concrete type for a case before.