Create a gist now

Instantly share code, notes, and snippets.

ScalaTest 2.0 RC1 with Play 2.2
name := "play22-with-scalatest2"
version := "1.0-SNAPSHOT"
libraryDependencies ++= Seq(
jdbc,
anorm,
cache,
"org.scalatest" % "scalatest_2.10" % "2.0.RC1" % "test->*"
)
play.Project.playScalaSettings
(testOptions in Test) += Tests.Argument(TestFrameworks.ScalaTest, "-h", "target/test-reports/html")
(testOptions in Test) += Tests.Argument(TestFrameworks.ScalaTest, "-u", "target/test-reports/xml")
import org.scalatest._
import org.scalatest.GivenWhenThen
import play.api.libs.json._
import play.api.test._
import play.api.test.Helpers._
class UserApiSpec extends FunSpec with GivenWhenThen with Matchers {
describe("Users API") {
describe("GET /users") {
it("should return all users") {
running(FakeApplication()) {
Given("no parameter")
val users = route(FakeRequest(GET, "/users")).get
Then("StatusCode is 200")
status(users) shouldBe OK
And("ContentType is application/json")
contentType(users).get shouldBe "applicatoin/json"
And("Content is all users's json")
contentAsString(users) shouldBe ("[...]")
}
}
}
describe("GET /users/:userId") {
it("should return single user") {
running(FakeApplication()) {
val userId = 1
Given(s"userId is ${userId}")
val user = route(FakeRequest(GET, s"/users/${userId}")).get
Then("StatusCode is 200")
status(user) shouldBe OK
And("ContentType is application/json")
contentType(user).get shouldBe "applicatoin/json"
val expected = """{ "userId": 1, "name": "john" }"""
val actualJson = Json.parse(contentAsString(user))
val expectedJson = Json.parse(expected)
Then("Content is a single users's json")
assert(actualJson \ "userId" === expectedJson \ "userId")
assert(actualJson \ "name" === expectedJson \ "name")
}
}
it("should return 404") {
running(FakeApplication()) {
Given("no existence userId")
val user = route(FakeRequest(GET, "/users/100")).get
Then("StatusCode is 404")
status(user) shouldBe NOT_FOUND
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment