Scala:使用Play框架进行模板测试

问题描述 投票:0回答:1

我正在从Java过渡到Scala。我正在寻找一种类似于:

的测试方法
//As a template is a just a method, you can execute it from a test and check the result:

@Test
public void renderTemplate() {
  Content html = views.html.index.render("Welcome to Play!");
  assertEquals("text/html", html.contentType());
  assertTrue(contentAsString(html).contains("Welcome to Play!"));
}

我在这里找到它:https://www.playframework.com/documentation/2.8.x/JavaTest任何在文档中找到此内容以尝试在Scala中编写类似测试的尝试均失败了。谁能帮忙?

scala playframework
1个回答
1
投票

这类测试在PlayFramework中称为Functional Tests是的,有记录良好的样本可以编写功能测试以及测试模板。以下代码示例是Scala中的替代版本

"render index template" in {
  val html = views.html.index("Hello")

  contentType(html) must equalTo("text/html")
  contentAsString(html) must contain("Welcome to Play!")
}

示例2。测试路由器:

"respond to the index Action" in {
  val Some(result) = routeAndCall(FakeRequest(GET, "/Bob"))

  status(result) must equalTo(OK)
  contentType(result) must beSome("text/html")
  charset(result) must beSome("utf-8")
  contentAsString(result) must contain("Hello Bob")
}
© www.soinside.com 2019 - 2024. All rights reserved.