Using Play 2.1-RC1 I can’t write simple test.
Here’s the action code:
def echoTestTagFromXml = Action(parse.xml) { request =>
(request.body \ "test" headOption).map(_.text).map { test =>
Ok(views.xml.testTag(test))
}.getOrElse {
BadRequest("Missing parameter [name]")
}
}
Here’s testing code:
"Test Tag Xml Echo" in {
running(FakeApplication()) {
val req = new FakeRequest(POST, controllers.routes.SimpleResultsController.echoTestTagFromXml().url, FakeHeaders(), Xml("<test>gg</test>"))
val result = controllers.SimpleResultsController.echoTestTagFromXml()(req)
status(result) must equalTo(OK)
}
}
Test gives the error:
[error] found : play.api.libs.iteratee.Iteratee[Array[Byte],play.api.mvc.Result]
[error] required: play.api.mvc.Result
From Google I know problem is in BodyParser. But I have no idea(after API investigation) how to make the code working.
The following modified testing code should work, but I think there is a bug at the moment when trying to pass a body into a FakeRequest, somewhat a hangover with the Functional tests now deprecated ‘routeAndCall’ function. The body is always empty.
I have a similar issue with passing Json into the body, but tried to get this working for your body parser (note the differences). Also, please set the content-type header.
You can however use the ‘route’ function instead:
This seems to work for me and you should be able to copy/paste this solution.
If you don’t want to repeat your route as a string then you can use the reverse routes as you did before:
controllers.routes.SimpleResultsController.echoTestTagFromXml().url