【发布时间】:2015-08-03 18:29:54
【问题描述】:
我正在尝试弄清楚如何使用 FsUnit 正确测试异常。官方文档指出,要测试异常,我必须纠正这样的事情:
(fun () -> failwith "BOOM!" |> ignore) |> should throw typeof<System.Exception>
但是,如果我没有用[<ExpectedException>] 属性标记我的测试方法,它总是会失败。听起来很合理,因为如果我们想测试异常,我们必须在 C# + NUnit 中添加这样的属性。
但是,只要我添加了这个属性,无论我试图抛出什么样的异常,它都会被处理。
一些sn-ps: 我的 LogicModule.fs
exception EmptyStringException of string
let getNumber str =
if str = "" then raise (EmptyStringException("Can not extract number from empty string"))
else int str
我的 LogicModuleTest.fs
[<Test>]
[<ExpectedException>]
let``check exception``()=
(getNumber "") |> should throw typeof<LogicModule.EmptyStringException>
【问题讨论】:
-
仅供参考 - 使用 Unquote,code.google.com/p/unquote,您会断言后一个示例中的
getNumber ""会引发预期的异常,例如raises<LogicModule.EmptyStringException> <@ getNumber "" @>