ScalaFunctionalTest - opensas/Play20Es GitHub Wiki
Dado que un template no es más que una función de Scala estándar, puede ejecutarlo desde sus pruebas y verificar el resultado:
"render index template" in {
val html = views.html.index("Coco")
contentType(html) must equalTo("text/html")
contentAsString(html) must contain("Hello Coco")
}Puede llamar a cualquier Action pasándole como parámetro un FakeRequest (aplicación simulada):
"respond to the index Action" in {
val result = controllers.Application.index("Bob")(FakeRequest())
status(result) must equalTo(OK)
contentType(result) must beSome("text/html")
charset(result) must beSome("utf-8")
contentAsString(result) must contain("Hello Bob")
}En vez de llamar a las Action usted mismo, puede dejar que lo haga el Router:
"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")
}A veces es conveniente ejecutar las pruebas con un servidor HTTP real, en ese caso puede iniciar un servidor:
"run in a server" in {
running(TestServer(3333)) {
await(WS.url("http://localhost:3333").get).status must equalTo(OK)
}
}Si desea probar su aplicación usando un explorador Web, puede usar Selenium WebDriver. Play iniciará WebDriver por usted, ofreciéndole una conveniente API provista por FluentLenium.
"run in a browser" in {
running(TestServer(3333), HTMLUNIT) { browser =>
browser.goTo("http://localhost:3333")
browser.$("#title").getTexts().get(0) must equalTo("Hello Guest")
browser.$("a").click()
browser.url must equalTo("http://localhost:3333/Coco")
browser.$("#title").getTexts().get(0) must equalTo("Hello Coco")
}
}Siguiente: Temas avanzados