我不确定如何用FsUnit.Xunit编写我的测试。
我想检查列表中是否至少有一个元素满足谓词。我知道如何使用should be True编写,但通常您可以通过使用专门的函数获得更好的错误消息。
希望这段代码清楚地说明了我想要实现的目标:
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发布于 2021-02-08 23:06:52
我会这样做:
xs
|> Seq.map (fun foo -> foo.X)
|> should contain 9错误输出非常有用:
Expected: Contains 8
Actual: seq [1; 4; 9]如果你想要更多的上下文,你可以这样做:
open FsUnit.CustomMatchers
...
xs |> should containf (fun foo -> foo.X = 9)错误输出为:
Expected: Contains <fun:squares@21-1>
Actual: [{ X = 1 }; { X = 4 }; { X = 9 }]这种方法唯一的缺点是"Expected“消息不再显示您正在寻找的特定值。
https://stackoverflow.com/questions/66099569
复制相似问题