Reputation: 37045
I am not sure how to write my test in FsUnit.Xunit
.
I want to check that at least one element in my list satisfies a predicate. I know how to write this using should be True
, but usually you can get better error messages by using the specialized functions.
Hopefully this code makes it clear what I am trying to achieve:
open Xunit
open FsUnit.Xunit
type Foo =
{
X : int
}
[<Fact>]
let ``squares`` () =
let xs =
[ { X = 1 }; { X = 2 }; { X = 3 } ]
|> List.map (fun foo -> { X = foo.X * foo.X })
actual
|> should exists (satisfies (fun foo -> foo.X = 9)) // Not real code
Upvotes: 3
Views: 163
Reputation: 17038
I would do this:
xs
|> Seq.map (fun foo -> foo.X)
|> should contain 9
Error output is useful:
Expected: Contains 8
Actual: seq [1; 4; 9]
If you want more context, you can do this instead:
open FsUnit.CustomMatchers
...
xs |> should containf (fun foo -> foo.X = 9)
Error output is:
Expected: Contains <fun:squares@21-1>
Actual: [{ X = 1 }; { X = 4 }; { X = 9 }]
The only downside of this approach is that the "Expected" message no longer displays the specific value you're looking for.
Upvotes: 3