使用 FsUnit 断言一些

Pau*_*icz 4 f# fsunit

我正在尝试检查我的函数是否返回 Some(x)

testedFunc() |> should be (sameAs Some)
testedFunc() |> should be Some
testedFunc() |> should equal Some
Run Code Online (Sandbox Code Playgroud)

都行不通。我宁愿不使用:

match testedFunc() with
    | Some -> Pass()
    | None -> Fail()
Run Code Online (Sandbox Code Playgroud)

有人知道这样做的方法吗?

Joe*_*ler 5

我还没有真正使用过 FsUnit,但像这样的东西应该可以工作......

testedFunc() |> Option.isSome |> should equal true
Run Code Online (Sandbox Code Playgroud)

或者因为一个 Option 已经有一个 IsSome 属性,你可以这样做,但要注意大小写 - 它与Option.isSome函数不同。

testedFunc().IsSome |> should equal true
Run Code Online (Sandbox Code Playgroud)

第三种方法是将您正在测试的函数组合在一起,Option.isSome以获得一个直接返回布尔值的函数。这在本示例中不是很有用,但是如果您需要使用各种输入多次测试返回 Option 的函数,这种方法可以帮助减少重复代码。

let testedFunc = testedFunc >> Option.isSome
testedFunc() |> should equal true
Run Code Online (Sandbox Code Playgroud)