Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a <a href="https://github.com/playframework/Play20/commit/818025bbd" rel="nofollow">fix for the integrated approach to this</a> in Playframewrk v2.1 I have a <a href="https://github.com/jeantil/Play20/commit/651b431fefe8365fd48d73fc495e362ef1ee3781" rel="nofollow">backport of the fix on the 2.0.x branch</a></p> <p>Until it gets merged and released, here is what I did (it works on Play 2.0.3+): </p> <p>I defined my own Helpers object in a libs package like so. </p> <pre><code>package libs import play.api.mvc._ import play.api.libs.iteratee._ import play.api.libs.concurrent._ import play.api.test._ object Helpers { def routeAndCall[T](request: FakeRequest[T]): Option[Result] = { routeAndCall(this.getClass.getClassLoader.loadClass("Routes").asInstanceOf[Class[play.core.Router.Routes]], request) } /** * Use the Router to determine the Action to call for this request and executes it. */ def routeAndCall[T, ROUTER &lt;: play.core.Router.Routes](router: Class[ROUTER], request: FakeRequest[T]): Option[play.api.mvc.Result] = { val routes = router.getClassLoader.loadClass(router.getName + "$").getDeclaredField("MODULE$").get(null).asInstanceOf[play.core.Router.Routes] routes.routes.lift(request).map { case a: Action[_] =&gt; val action = a.asInstanceOf[Action[T]] val parsedBody: Option[Either[play.api.mvc.Result, T]] = action.parser(request).fold( (a, in) =&gt; Promise.pure(Some(a)), k =&gt; Promise.pure(None), (msg, in) =&gt; Promise.pure(None) ).await.get parsedBody.map{resultOrT =&gt; resultOrT.right.toOption.map{innerBody =&gt; action(FakeRequest(request.method, request.uri, request.headers, innerBody)) }.getOrElse(resultOrT.left.get) }.getOrElse(action(request)) } } } </code></pre> <p>Then in my test I import my Helpers and the whole play Helpers context, except for routeAndCall : </p> <pre><code>import libs.Helpers._ import play.api.test.Helpers.{routeAndCall =&gt; _,_} </code></pre> <p>I then use an Around to setup my app (I need the provide an application.secret as I store the authenticated user name in the session which is based on a signed cookie) </p> <pre><code>def appWithSecret():Map[String,String]={ Map(("application.secret","the answer is 42 !")) } object emptyApp extends Around { def around[T &lt;% Result](t: =&gt; T) = { running(FakeApplication(additionalConfiguration = inMemoryMongoDatabase("emptyApp")++appWithSecret())) { User(new ObjectId, "Jane Doe", "foobar@example.com", "id1").save() t // execute t inside a http session } } } </code></pre> <p>This allows me to write the following tests: </p> <pre><code>"respond to the index Action" in emptyApp { val request: FakeRequest[AnyContent] = FakeRequest(GET, "/expenses").withSession(("email", "foobar@example.com")) val Some(result) = routeAndCall(request) status(result) must equalTo(OK) contentType(result) must beSome("application/json") charset(result) must beSome("utf-8") contentAsString(result) must contain("Hello Bob") } </code></pre> <p>It allows you to exercise the secured code even though it is not a unit test.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload