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'
所以我更新了我的答案以匹配它。