【问题标题】:Assert Some with FsUnit用 FsUnit 断言一些
【发布时间】:2012-04-09 17:54:00
【问题描述】:

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

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

一切都不起作用。我宁愿不使用:

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

有人知道怎么做吗?

【问题讨论】:

标签: f# fsunit


【解决方案1】:

我还没有真正使用过 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2017-11-07
    • 2018-11-22
    • 2014-10-21
    • 2018-04-28
    • 2013-04-06
    • 2022-09-28
    相关资源
    最近更新 更多