如何在一个 Play 2 specs2 单元测试中执行多个请求

How to perform multiple request in one Play 2 specs2 unit test

我有一个使用 Play 2 框架的简单网络应用程序。它有两个 REST API:

我想对其进行功能测试。我希望测试多次调用 /write,然后验证 /read.

的结果

但是 route 函数 returns 一个 Future,我找不到让 specs2 等待我的方法Future.

我的代码如下:

object MySpec extends Specification {

  "/write * 2, then /read" in new WithApplication {
    val write1 = route(app, FakeRequest(GET, '/write')).get
    val write2 = route(app, FakeRequest(GET, '/write')).get
    val read = route(app, FakeRequest(GET, '/read')).get

    // how to chain the requests so the execute one after another, and the specs2 can wait for it?

    status(read) must_==OK
  }
}

你不能做这样的事情吗?

import play.api.mvc._
import play.api.test._
import scala.concurrent.Future

object MySpec extends Specification {

"/write * 2, then /read" in new WithApplication {
   val result = for{
     _ <- route(app, FakeRequest(GET, '/write'))
     _ <- route(app, FakeRequest(GET, '/write'))
     read <- route(app, FakeRequest(GET, '/read'))
   }yield{ read }

   status(result) mustEqual OK
}