forked from playframework/playframework
-
Notifications
You must be signed in to change notification settings - Fork 0
ScalaFunctionalTest
maffoo edited this page Feb 29, 2012
·
4 revisions
Since a template is a standard Scala function, you can execute it from your test, and check the result:
"render index template" in {
val html = views.html.index("Coco")
contentType(html) must equalTo("text/html")
contentAsString(html) must contain("Hello Coco")
}
You can call any Action
code by providing a FakeRequest
:
"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")
}
Instead of calling the Action
yourself, you can let the Router
do it:
"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")
}
Sometimes you want to test the real HTTP stack from with your test, in which case you can start a test server:
"run in a server" in {
running(TestServer(3333)) {
await(WS.url("http://localhost:3333").get).status must equalTo(OK)
}
}
If you want to test your application using a browser, you can use Selenium WebDriver. Play will start the WebDriver for your, and wrap it in the convenient API provided by 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")
}
}
Next: Advanced topics
- HTTP programming
- Asynchronous HTTP programming
- The template engine
- HTTP form submission and validation
- Working with JSON
- Working with XML
- Handling file upload
- Accessing an SQL database
- Using the Cache
- Calling WebServices
- Integrating with Akka
- Internationalization
- The application Global object
- Testing your application