I want to test that my application is rendering the right template in play framework 2.3.x, something like:
"Index" should{
"render index template" in new WithApplication{
...
val result = call(controller.index, FakeRequest())
someFunction(result) must render(views.html.index)
}
}
Something similar to what rspec does:
response.should render_template("success")
Is it possible, what's the recommended way in play?
For a simple controller function like that, I would check the content of the response, versus the content of the rendered template. Note that a template is rendered as Html
, so you must call toString
to compare with the content of the Result
. I like to check the content type and status of the Result
as well.
"Index" should {
"render index template" in new WithApplication {
val request = FakeRequest(GET, '/') // I prefer using the router, but it doesn't matter that much.
val Some(result) = route(request)
val expectedContent = views.html.index().toString
contentAsString(result) must equalTo(expectedContent)
contentType(result) must equalTo("text/html")
status(result) must equalTo(OK)
}
}