【问题标题】:Scala : Template testing with Play frameworkScala:使用 Play 框架进行模板测试
【发布时间】:2020-04-09 23:16:45
【问题描述】:

我正在从 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】:

    这类测试在 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")
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-24
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 2017-11-14
      • 2017-08-02
      相关资源
      最近更新 更多