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

时间:2020-04-09 23:16:45

标签: scala playframework

我正在从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中编写类似测试的任何尝试均以失败告终。有人可以帮忙吗?

1 个答案:

答案 0 :(得分: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")
}
相关问题