F# 如何使用FsUnit正确测试异常
我正试图找出如何正确地使用FsUnit测试异常。官方文件指出,要测试异常,我必须纠正如下内容:F# 如何使用FsUnit正确测试异常,f#,fsunit,F#,Fsunit,我正试图找出如何正确地使用FsUnit测试异常。官方文件指出,要测试异常,我必须纠正如下内容: (fun () -> failwith "BOOM!" |> ignore) |> should throw typeof<System.Exception> 我的LogicModuleTest.fs [<Test>] [<ExpectedException>] let``check exception``()= (getNumber "
(fun () -> failwith "BOOM!" |> ignore) |> should throw typeof<System.Exception>
我的LogicModuleTest.fs
[<Test>]
[<ExpectedException>]
let``check exception``()=
(getNumber "") |> should throw typeof<LogicModule.EmptyStringException>
[]
[]
让``检查异常``()=
(getNumber“”)|>应抛出typeof
如果要测试某个代码是否引发了特定的异常类型,可以将异常类型添加到[]
属性中,如下所示:
[<Test; ExpectedException(typeof<LogicModule.EmptyStringException>)>]
let``check exception`` () : unit =
(getNumber "")
|> ignore
[]
让``检查异常``():单位=
(getNumber“”)
|>忽略
NUnit网站上提供了更多文档:已找到答案。要测试引发的异常,我应该使用下一种样式包装我的函数调用:
(fun () -> getNumber "" |> ignore) |> should throw typeof<LogicModule.EmptyStringException>
(fun()->getNumber”“|>ignore)|>应该抛出typeof
因为下面#fsunit使用NUnit的抛出约束
…它接受一个void,raise的委托,返回'a谢谢你的回答,但我不喜欢添加一些额外属性的想法,因为当你使用FsUnit.FYI时,它看起来不太好-如果不加引号,你会断言
getNumber“”
在后一个示例中,引发了预期的异常,如引发了
很好的回答-但我认为您不需要ExpectedException属性。请注意,需要从lambda返回单位(即,以“|>ignore”结束有趣的定义)。
(fun () -> getNumber "" |> ignore) |> should throw typeof<LogicModule.EmptyStringException>