class UserControllerSpec extends PlaySpecification {
"User Controller" should {
"successfully register a new user." in {
implicit val app = FakeApplication()
running(app) {
val response = route(FakeRequest(
Helpers.POST,
com.qburst.omnia.api.controllers.routes.UserController.signUp().url,
FakeHeaders(Seq(CONTENT_TYPE -> Seq("application/json"))),
Json.obj("email" -> "user@test.com",
"password" -> "testpassword"))).get
status(response) must equalTo(OK)
contentType(response) must beSome("application/json")
charset(response) must beSome("utf-8")
val responseNode = Json.parse(contentAsString(response))
(responseNode \ "status").as[Int] must equalTo(1)
}
}
}
}
我想在执行一组此类测试之前创建Cassandra键空间,并在执行测试后删除Cassandra键空间。怎么办呢?
答案 0 :(得分:4)
我只是注意到specs2库已经有了'BeforeAfterAll'让你做到的特质。尝试以下内容:
import org.specs2.specification.BeforeAfterAll
class UserControllerSpec extends PlaySpecification with BeforeAfterAll{
override def beforeAll(): Unit = //Initialize Cassandra here
override def afterAll(): Unit = //Release Cassandra here
"User Controller" should {
"successfully register a new user." in {
implicit val app = FakeApplication()
running(app) {
val response = route(FakeRequest(
Helpers.POST,
com.qburst.omnia.api.controllers.routes.UserController.signUp().url,
FakeHeaders(Seq(CONTENT_TYPE -> Seq("application/json"))),
Json.obj("email" -> "user@test.com",
"password" -> "testpassword"))).get
status(response) must equalTo(OK)
contentType(response) must beSome("application/json")
charset(response) must beSome("utf-8")
val responseNode = Json.parse(contentAsString(response))
(responseNode \ "status").as[Int] must equalTo(1)
}
}
}
}
您可以在最新版本的specs2库中找到它。我不确定它添加了哪个版本。
答案 1 :(得分:0)
快速更新:
如果您需要在 playframework 2.6.x 上运行此功能 你可以用
import org.scalatestplus.play.PlaySpec
import org.scalatest.BeforeAndAfterAll
class UserControllerSpec extends PlaySpec with BeforeAndAfterAll{
override def beforeAll(): Unit = //code to run before all tests starts
override def afterAll(): Unit = //code to run after all tests finishes
"User Controller" should {
"successfully register a new user." in {
//blabla
}
}
}
我希望这有助于某人;)