Google Groups

Re: [play-2.1-scala] problem with tests - FakeRequest and json

James Roper Nov 19, 2012 4:32 PM
Posted in group: Play Framework [deprecated]
I've got a better solution for you.

Assuming this is your controller:

package controllers
object User {
def create(username: String) = Action(parse.json) { req =>
// Do something

And here is your route

POST   /user/:username    controllers.User.create(username)

Then you can:

val json = Json.obj(
"email" -> JsString(""),
"password" -> JsString("fakepassword")

val req
= FakeRequest().copy(body = json)

val result
= controllers.User.create("test")(req)
(result) must equalTo(OK)

That way you don't need to worry about the method or URI.

On Tuesday, 20 November 2012 10:15:14 UTC+11, meloniasty wrote:

I'm getting problems with testing controller's method where input parameters are in json, for example:

val json = Json.obj(
          "email" -> JsString(""),
          "password" -> JsString("fakepassword")

val req = FakeRequest(
          method = "POST",
          uri = "/user/test",
          headers = FakeHeaders(
          body =  json

val result = route(req).get
status(result) must equalTo(OK)

and response content is always Bad Request - Invalid JSON

my routing is
POST /user/test controllers.user.test

if i run play in server mode and test via any rest browser client it's work perfectly

maybe its bug in 2.1 or i'm doing sth wrong ?

best regards