我想检查一个值是否属于受歧视联合的特定情况,而不必检查任何包含的数据。我的动机是每个单元测试只测试一件事。
一个例子如下(最后两行给出编译错误):
module MyState
open NUnit.Framework
open FsUnit
type MyState =
| StateOne of int
| StateTwo of int
let increment state =
match state with
| StateOne n when n = 10 -> StateTwo 0
| StateOne n -> StateOne (n + 1)
| StateTwo n -> StateTwo (n + 1)
[<Test>]
let ``incrementing StateOne 10 produces a StateTwo`` ()=
let state = StateOne 10
(increment state) |> should equal (StateTwo 0) // works fine
(increment state) |> should equal (StateTwo _) // I would like to write this...
(increment state) |> should be instanceOfType<StateTwo> // ...or this
这可以在 FsUnit 中完成吗?
我知道这个答案,但不想为每种情况编写匹配函数(在我的真实代码中,有两个以上)。