4

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

testedFunc() |> should be (sameAs Some)
testedFunc() |> should be Some
testedFunc() |> should equal Some

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

match testedFunc() with
    | Some -> Pass()
    | None -> Fail()

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

4

1 回答 1

6

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

testedFunc() |> Option.isSome |> should equal true

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

testedFunc().IsSome |> should equal true

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

let testedFunc = testedFunc >> Option.isSome
testedFunc() |> should equal true
于 2012-04-09T18:29:06.643 回答