如何断言异常是预期的

How to assert an exception is expected

我正在 Mac 运行 F# 使用 .NET Core 2.0。

我有一个看起来像这样的函数:

let rec evaluate(x: string) =
  match x with
  // ... cases
  | _ -> failwith "illogical"

我想编写一个 Expecto 测试来验证异常是否按预期抛出,大致如下:

// doesn't compile
testCase "non-logic" <| fun _ ->
  Expect.throws (evaluate "Kirkspeak") "illogical" 

错误是

This expression was expected to have type 'unit -> unit' but here has type 'char'

unit -> unit 让我觉得这类似于 Assert.Fail,这不是我想要的。

作为 F# 和 Expecto 的新手,我无法找到断言按预期抛出异常的工作示例。有人有吗?

Expect.throws 具有签名 (unit -> unit) -> string -> unit 因此您要测试的函数必须是 (unit -> unit) 或包含在 (unit -> unit) 的函数中。

let rec evaluate (x: string) : char =
  match x with
  // ... cases
  | _ -> failwith "illogical"

编译器错误告诉您传递给 Expect.throws 的函数还没有正确的签名。

[<Tests>]
let tests = testList "samples" [
    test "non-logic" {
      // (evaluate "Kirkspeak") is (string -> char)
      // but expecto wants (unit -> unit)
      Expect.throws (evaluate "Kirkspeak") "illogical"
    }
]

[<EntryPoint>]
let main argv =
    Tests.runTestsInAssembly defaultConfig argv

使其工作的一种方法是更改​​

Expect.throws (evaluate "Kirkspeak") "illogical"

// you could instead do (fun () -> ...)
// but one use of _ as a parameter is for when you don't care about the argument
// the compiler will infer _ to be unit
Expect.throws (fun _ -> evaluate "Kirkspeak" |> ignore) "illogical"

现在 expecto 很开心!

这个答案是我思考的方式。遵循类型签名通常很有帮助。

编辑:我看到你的错误消息说 This expression was expected to have type 'unit -> unit' but here has type 'char' 所以我更新了我的答案以匹配它。