我想弄清楚如何正确测试异常与FsUnit。官方文件状态,即要测试异常我有合适的是这样的:如何正确测试例外与FsUnit
(fun() -> failwith "BOOM!" |> ignore) |> should throw typeof<System.Exception>
但是,如果我没有记住我的测试方法与[<ExpectedException>]
属性,它会永远失败。听起来很合理,因为如果我们想测试异常,我们必须在C#+ NUnit中添加这样的属性。
但是,只要我添加了这个属性,我试图抛出什么样的异常并不重要,它总是会被处理。
一些片段: 我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>
FYI - 使用Unquote,https://code.google.com/p/unquote/,你会断言你的后一个例子中的'getNumber“”'会引发预期的异常,比如'raise<@ getNumber“”@ >' –
2013-04-28 13:18:39