David S.
David S.

Reputation: 11178

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

I have a simple web app using Play 2 framework. It has two REST API:

I want to implement a functional test for it. I want the test to invoke the /write multiple times, then verify the result of /read.

But the route function returns a Future, and I could not find a way to let the specs2 to wait for my Future.

My code looks like:

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
  }
}

Upvotes: 0

Views: 56

Answers (1)

Tomer
Tomer

Reputation: 2448

Can't you do something like this?

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
}

Upvotes: 1

Related Questions