测试F#异步工作流程与xUnit.net的任务支持 [英] Testing F# async workflows with xUnit.net's Task support

查看:173
本文介绍了测试F#异步工作流程与xUnit.net的任务支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写F#code和测试在的xUnit 1.9

I'm writing F# code and tests in xUnit 1.9.

有关正常同步的东西,我只是返回单元,一切都很好;但现在,我迁移同步内脏是异步的工作流程。

For normal sync stuff, I simply return unit and all is well; but now, I'm migrating the sync innards to be async workflows.

在换句话说,我简单的AAA越来越明确的异步使用挤压到它,因为我重构系统:

In other words, my simple AAA is getting explicit Async usage pushed out into it as I refactor the system:

let [<Fact>] ``Can consume using NEventStore InMemory`` () = 
    let store = NesGateway.createInMemory ()

    let finalDirection = playCircuit store |> Async.RunSynchronously // <----- YUCK

    test <@ CounterClockWise = finalDirection @> 

要解决这个问题,我想使测试的主体是异步。然而,就我所知的,xUnit.net只管理返回工作派生类型的方法,因此我需要有一个安全的方式我的异步试验机构进行适当包装的xUnit.net亚军把它捡起来并适当地处理它。

To remedy this, I want to make the body of the test be async. However, to the best of my knowledge, xUnit.net only manages methods returning Task-derived types, and hence I need a safe way of having my async test body be wrapped suitably for xUnit.net's runner to pick it up and handle it appropriately.

什么是EX preSS我的上述测试的最好方式?

What's the best way to express my above test ?

推荐答案

您也可以创建自己的计算前pression封装 Async.RunSynchronously 电话:

You can also create your own computation expression to encapsulate the Async.RunSynchronously call:

type AsyncTestBuilder() =
  member this.Bind (v: Async<'a>, c: 'a -> 'b) = v |> Async.RunSynchronously |> c;
  member this.Zero () = ()

let asyncTest = AsyncTestBuilder() 

let [<Fact>] ``Can consume using NEventStore InMemory`` () = asyncTest {
  let store = NesGateway.createInMemory ()

  let! finalDirection = playCircuit store  // <-- use let! instead of let

  test <@ CounterClockWise = finalDirection @> }

这篇关于测试F#异步工作流程与xUnit.net的任务支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆