【问题标题】:How to execute a set of code before and after controller tests written using PlaySpecification?如何在使用 PlaySpecification 编写的控制器测试之前和之后执行一组代码?
【发布时间】:2016-04-15 19:20:36
【问题描述】:
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 键空间。如何做到这一点?

【问题讨论】:

    标签: scala playframework cassandra


    【解决方案1】:

    我刚刚注意到 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 库中找到它。我不确定它是在哪个版本中添加的。

    【讨论】:

      【解决方案2】:

      快速更新:

      如果您需要在 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
              }
       }
      
      }
      

      我希望这对某人有所帮助;)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-16
        • 2013-12-04
        • 2013-01-24
        • 1970-01-01
        相关资源
        最近更新 更多