FsUnit `should equal` fails on` Some [] `

When I run this FsUnit test with NUnit 2.6.3,

let f xs = Some (List.map ((+) 2) xs)

[<Test>]
let test() =
  f []
  |> should equal (Some [])

I get:

Result Message: 
Expected: <Some([])>
  But was:  <Some([])>
Result StackTrace:  
at FsUnit.TopLevelOperators.should[a,a](FSharpFunc`2 f, a x, Object y)

The test does not work, even if the expected and actual in the message match. What happened?

+4
source share
2 answers

The reason is that FsUnit uses an untyped mechanism under the hood, therefore it Expectedis displayed as objectusing type checking (see the part Object yon the stack).

The workaround is to add type annotations for generic values ​​i.e.

[<Test>]
let test() =
  f []
  |> should equal (Some ([]: int list))

Several people have been bitten by this, for example, Weird No behavior in type providers .

, . :

let shouldEqual (x: 'a) (y: 'a) = 
    Assert.AreEqual(x, y, sprintf "Expected: %A\nActual: %A" x y)
+7

, . . actual expected :

[<Test>]
let test() =
  let expected = Some []
  let actual = f []
  actual |> should equal expected

expected 'a list option, int list option, .

, .

[<Test>]
let test() =
  f []
  |> should equal (Some List.empty<int>)
+4

Source: https://habr.com/ru/post/1542885/


All Articles