Testing F#:单元测试用例失败
假设我有一个类似于Testing F#:单元测试用例失败,testing,f#,Testing,F#,假设我有一个类似于newConvert的函数。我收到newconvertor(“IL”)的错误。要生成此erorr:我使用了failwith“Invalid Input” erorr是: System.Exception: Invalid Input > at FSI_0160.inputChecker(FSharpList`1 numberList) in C:\Users\Salman\Desktop\coursework6input\itt8060-master\coursew
newConvert
的函数。我收到newconvertor(“IL”)
的错误。要生成此erorr:我使用了failwith“Invalid Input”
erorr是:
System.Exception: Invalid Input
> at FSI_0160.inputChecker(FSharpList`1 numberList) in C:\Users\Salman\Desktop\coursework6input\itt8060-master\coursework6input\BrokenRomanNumbers\Library1.fs:line 140
at FSI_0160.newCovertor(String romanNumber) in C:\Users\Salman\Desktop\coursework6input\itt8060-master\coursework6input\BrokenRomanNumbers\Library1.fs:line 147
at <StartupCode$FSI_0165>.$FSI_0165.main@() in C:\Users\Salman\Desktop\coursework6input\itt8060-master\coursework6input\BrokenRomanNumbers\newtest.fs:line 32
Stopped due to error
我不能理解!!函数正确地失败了,但测试不接受它,那么为什么
newconvertor
不会生成系统。Execption
-它会抛出它-因此您永远无法到达应该等于…
的部分
要使用FsUnit捕获异常,必须将其包装为操作:
(fun () -> newCovertor("IL") |> ignore) |> should throw typeof<System.Exception>
(fun()->newCovertor(“IL”)|>ignore)|>应该抛出typeof
还可以看到-有很多方法可以实现这一点
您的版本不起作用的原因是,NUnit通常会在测试方法体中抛出异常时将测试标记为失败,而您是这样做的 如果将其包装在操作中,则
应抛出的可以选择在中执行此延迟操作,然后重试。。。匹配
块以捕获预期的异常并将其过滤掉(在这种情况下,如果没有异常,则抛出异常)
顺便说一句:就像在C#/VB.net中一样,如果您愿意,您也可以在方法级别上使用from NUnit-这样测试框架将为您处理它
(fun () -> newCovertor("IL") |> ignore) |> should throw typeof<System.Exception>