如何测试正确模板的渲染

时间:2014-08-28 22:13:36

标签: scala testing playframework-2.0 specs2

我想测试我的应用程序在play framework 2.3.x中呈现正确的模板,如:

"Index" should{
    "render index template" in new WithApplication{
      ...
      val result = call(controller.index, FakeRequest())
      someFunction(result) must render(views.html.index)
    }
}

与rspec类似的东西:

response.should render_template("success")

是否有可能,推荐的游戏方式是什么?

1 个答案:

答案 0 :(得分:1)

对于这样的简单控制器函数,我会检查响应的内容,而不是渲染模板的内容。请注意,模板呈现为Html,因此您必须调用toString以与Result的内容进行比较。我也想查看Result的内容类型和状态。

"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)
    }
}