manuelbernhardt / reactive-web-applications

Sources of the sample applications of the Reactive Web Application book
http://manning.com/books/reactive-web-applications
180 stars 65 forks source link

chapter 5 - Testing futures #2

Closed lucianenache closed 8 years ago

lucianenache commented 9 years ago

For the testing futures with specs2, if one is using play2.4 and also specs3.5, in order to be able to use the await method when testing futures, one should import the specs2 execution environment and add that to each block implicitly:

import org.specs2.matcher.Matchers
import org.specs2.mutable.Specification
import org.specs2.time.NoTimeConversions
import scala.concurrent.duration._

import org.specs2.concurrent.ExecutionEnv

class AuthenticationServiceSpec extends Specification with Matchers with NoTimeConversions {

  "The AuthenticationService" should {

    val service: AuthenticationService = new DummyAuthenticationService

    "correctly authenticate Bob Marley" in { implicit ee: ExecutionEnv =>
      service.authenticateUser("bob@marley.org", "secret") must beEqualTo (AuthenticationSuccessful).await(1, 200.millis)
    }

    "not authenticate Ziggy Marley" in { implicit ee: ExecutionEnv =>
      service.authenticateUser("ziggy@marley.org", "secret") must beEqualTo (AuthenticationUnsuccessful).await(1, 500.millis)
    }

    "fail if it takes too long" in { implicit ee: ExecutionEnv =>
      service.authenticateUser("jimmy@hendrix.com", "secret") must throwA[RuntimeException].await(1, 700.millis)
    }
  }
}
manuelbernhardt commented 8 years ago

@lucianenache thanks, I fixed that when upgrading the code