ded*_*ed' 5 f# asynchronous expecto
我已开始使用 F# Expecto 框架,但找不到正确的方法来断言异步计算表达式中的异常。
我的代码最终是:
Expect.throws (fun () ->
async {
// Some async code expected to fail...
do! Async.Sleep 1
failwith "error"
}
|> Async.RunSynchronously
|> ignore) "valuable message"
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?(没有 RunSynchronously 并忽略)。
谢谢
Expecto 中似乎尚未内置此组合器,但您可以使用自己的组合器对其进行扩展:
module Expect =
let throwsAsync (code: unit -> Async<unit>) (message: string) = async {
let! thrown = async {
// Using a try-catch and a lambda is better than Async.Catch, because there are otherwise some circumstances where exceptions don't get caught
try
//
do! code ()
return false
with _ ->
return true
}
if not thrown then
failtestf "%s. Expected f to throw." message
}
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
let f () = async {
// Some async code expected to fail...
do! Async.Sleep 1
failwith "error"
}
testCaseAsync "Some test" (async {
do! Expect.throwsAsync (fun () -> f ()) "Should throw"
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
97 次 |
| 最近记录: |