不能使用xUnit测试f#中的异常



代码如下:

module Tests
open System
open Xunit
[<Fact>]
let ``Simple Test`` () =
Assert.Throws<Exception>(failwith "Error")

使用

编译失败
error FS0041: A unique overload for method 'Throws' could not be determined based on type information prior to this program point. A type annotation may be needed. Known type of argument: 'a  Candidates: - Assert.Throws<'T when 'T :> exn>(testCode: Action) : 'T - Assert.Throws<'T when 'T :> exn>(testCode: Func<Threading.Tasks.Task>) : 'T - Assert.Throws<'T when 'T :> exn>(testCode: Func<obj>) : 'T

我做错了什么?

它在寻找一个Action或Func,你可以使用f# lambda,编译器会隐式地为你创建一个Func。

module Tests
open System
open Xunit
[<Fact>]
let ``My test`` () =
Assert.Throws<Exception>(
fun () -> 
(failwith "Error") :> obj)

在这个例子中,你必须强制转换输出来告诉编译器函数的返回类型是什么(它可以是任何类型,因为它不返回任何东西)

相关内容

  • 没有找到相关文章

最新更新