【发布时间】:2015-10-06 13:02:18
【问题描述】:
我想对一个接受 DU 并返回另一个的函数做一些单元测试:
type Commands =
| Schedule of string
| Abandon of string
type Events =
| Scheduled of string
| Abandonned of string
函数如下:
let exec state = function
| Schedule (cmd) -> Choice1Of2( Scheduled("yes"))
| Abandon(cmd) -> Choice1Of2( Abandonned ("no"))
我的测试如下:
let result:Choice<Events,string> = exec "initial state" <| Schedule("myCommand");;
result 具有以下类型 Choice<Events,string>,我很想获得一些快速功能以便像这样使用它们:
assertEvent Scheduled (fun e -> Assert.Equal("should produce GameScheduled Event",gameScheduled, e)) result
但要做到这一点,我将拥有以下自制断言功能:
let assertEvent<'TEvent> f g result =
match result with
| Choice1Of2(e) ->
match e with
| f(evt) -> g(evt)
| _ -> Assert.None("event not recognised",Some(e))
| Choice2Of2(reason) -> Assert.None("reason",Some(reason))
我期待函数 f 允许动态匹配模式,但事实并非如此。相反,我有以下错误:
The pattern disciminator 'f' is not defined
我在某处做错了吗?我的fsharp技能没那么高……
【问题讨论】:
标签: f#