Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Testing F#:单元测试用例失败_Testing_F# - Fatal编程技术网

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>