gpt4 book ai didi

scala - 如何测试正确模板的渲染

转载 作者:行者123 更新时间:2023-11-28 20:42:49 25 4
gpt4 key购买 nike

我想测试我的应用程序是否在 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")

可以吗,推荐的玩法是什么?

最佳答案

对于像这样的简单 Controller 功能,我会检查响应的内容与渲染模板的内容。请注意,模板呈现为 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)
}
}

关于scala - 如何测试正确模板的渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25558847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com