Dissecting PlaySpecification

The tests written using Specs2 can also be written as follows:

class UtilSpec extends PlaySpecification {...}

PlaySpecification is a trait that provides the required helper methods to test a Play application using Specs2. It is defined as:

trait PlaySpecification extends Specification
    with NoTimeConversions
    with PlayRunners
    with HeaderNames
    with Status
    with HttpProtocol
    with DefaultAwaitTimeout
    with ResultExtractors
    with Writeables
    with RouteInvokers
    with FutureAwaits {
}

Let's scan through the API exposed by each of these traits to understand its significance:

  • Specification and NoTimeConversions are traits of Specs2. NoTimeConversions can be used to deactivate the time conversions.
  • PlayRunners provides helper methods to execute a block of code in a running application or server with or without specifying the browser.
  • HeaderNames and Status define constants for all the standard HTTP headers and HTTP status codes, respectively, with their relevant names, as shown here:
    HeaderNames.ACCEPT_CHARSET = "Accept-Charset"
    Status.FORBIDDEN = 403
    • HttpProtocol defines the constants related to the HTTP protocol:
      object HttpProtocol extends HttpProtocol
      trait HttpProtocol {
        // Versions
        val HTTP_1_0 = "HTTP/1.0"
      
        val HTTP_1_1 = "HTTP/1.1"
      
        // Other HTTP protocol values
        val CHUNKED = "chunked"
      }
    • ResultExtractors provides methods to extract data from the HTTP response, which is of the Future[Result] type. These methods are as follows:
      • charset(of: Future[Result])(implicit timeout: Timeout): Option[String]
      • contentAsBytes(of: Future[Result])(implicit timeout: Timeout): Array[Byte]
      • contentAsJson(of: Future[Result])(implicit timeout: Timeout): JsValue
      • contentAsString(of: Future[Result])(implicit timeout: Timeout): String
      • contentType(of: Future[Result])(implicit timeout: Timeout): Option[String]
      • cookies(of: Future[Result])(implicit timeout: Timeout): Cookies
      • flash(of: Future[Result])(implicit timeout: Timeout): Flash
      • header(header: String, of: Future[Result])(implicit timeout: Timeout): Option[String]
      • headers(of: Future[Result])(implicit timeout: Timeout): Map[String, String]
      • redirectLocation(of: Future[Result])(implicit timeout: Timeout): Option[String]
      • session(of: Future[Result])(implicit timeout: Timeout): Session
      • status(of: Future[Result])(implicit timeout: Timeout): Int

    The implicit Timeout in these method calls is provided by the DefaultAwaitTimeout trait and the default timeout is set to 20 seconds. This can be overridden by providing an implicit timeout in the scope of the scenario.

  • RouteInvokers provides the methods to call a corresponding Action for a given request using Router. These methods are as follows:
    • route[T](app: Application, req: Request[T])(implicit w: Writeable[T]): Option[Future[Result]]
    • route[T](app: Application, rh: RequestHeader, body: T)(implicit w: Writeable[T]): Option[Future[Result]]
    • route[T](req: Request[T])(implicit w: Writeable[T]): Option[Future[Result]]
    • route[T](rh: RequestHeader, body: T)(implicit w: Writeable[T]): Option[Future[Result]]
    • call[T](action: EssentialAction, rh: RequestHeader, body: T)(implicit w: Writeable[T]): Future[Result]
    • call[T](action: EssentialAction, req: FakeRequest[T])(implicit w: Writeable[T]): Future[Result]

    The implicit Writable in these method calls is provided by the Writeables trait. The call methods are inherited from EssentialActionCaller.

  • The FutureAwaits trait provides methods to wait on a request with or without specifying the waiting time.

Note

Although the library that supports ScalaTest for a Play application has an PlaySpec abstract class, there is no equivalent to PlaySpecification for ScalaTest. Instead, there's a helper object, which is defined as follows:

object Helpers extends PlayRunners
  with HeaderNames
  with Status
  with HttpProtocol
  with DefaultAwaitTimeout
  with ResultExtractors
  with Writeables
  with EssentialActionCaller
  with RouteInvokers
  with FutureAwaits

PlaySpec is defined as follows:

abstract class PlaySpec extends WordSpec with MustMatchers with OptionValues with WsScalaTestClient

Hence, importing play.api.test.Helpers is also sufficient to use only the helper methods.

For the following sections, with regard to tests using Specs2, we will extend PlaySpecification, and for ScalaTest, we will assume that play.api.test.Helpers is imported and the test extends to PlaySpec.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.164.141