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

时间:2017-03-23 09:56:13

标签: unit-testing playframework-2.0 specs2

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

  • /写
  • /读

我想为它实现功能测试。我希望测试多次调用/write,然后验证/read的结果。

route函数返回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
  }
}

1 个答案:

答案 0 :(得分:1)

你不能做这样的事吗?

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
}