Search code examples
scalatestingplayframework-2.0specs2

How to test redirect location with reverse routing?


How can I test if a controller redirects to a specific location using the reverse routing in play framework 2.3? I would like to do something like this:

  "LoginController#authenticate" should{
      "Redirect to index on success" in{
          ...
          val result = loginControllerTest.authenticate.apply(request)
          redirectLocation(result) must be(routes.Application.index)
   }

Solution

  • routes.Application.index is a Call which holds a method (GET, POST, PUT, DELETE) and a url.

    redirectLocation(result) returns an Option[String] (None if there is no redirect)

    You would want something like:

    redirectLocation(result) must beSome(routes.Application.index.url)
    

    routes.Application.index.toString would do the same.