如何在 play 框架应用程序中进行完整的集成测试?

How to do full integration tests in a play framework app?

我正在尝试为使用 play framework 2.4 和 scala 编写的应用程序编写测试。来源可用here

我正在尝试编写适用于整个堆栈的测试。我们正在使用可通过 REST API.

获得的 neo4j 数据库

这是我的测试:

package integration

import org.specs2.mutable._
import org.specs2.specification._
import play.api.test.Helpers._
import play.api.test._
import setup.TestSetup

class PublicAPISpec extends Specification with BeforeEach {

  def before = TestSetup.populateNeo4JData()

  "The score feedback badge api" should {
    "deliver a correct svg file" in {
      running(FakeApplication()) {
        val svg = route(FakeRequest(GET, "/api/badges/github/test/test.svg")).get

        status(svg) must equalTo(OK)
        contentType(svg) must beSome.which(_ == "image/svg+xml")
      }
    }
  }
}

这是我的堆栈跟踪:

[info] The score feedback badge api should
[error]   ! deliver a correct svg file
[error]    There is no started application (Play.scala:71)
[error] play.api.Play$$anonfun$current.apply(Play.scala:71)
[error] play.api.Play$$anonfun$current.apply(Play.scala:71)
[error] play.api.Play$.current(Play.scala:71)
[error] setup.TestSetup$.setup$TestSetup$$cypher(TestSetup.scala:80)
[error] setup.TestSetup$.clearNeo4JData(TestSetup.scala:23)
[error] setup.TestSetup$.populateNeo4JData(TestSetup.scala:32)
[error] integration.PublicAPISpec.before(PublicAPISpec.scala:15)
[error] integration.PublicAPISpec.before(PublicAPISpec.scala:13)

编辑 事实证明,就像堆栈跟踪一样,明显指出了 TestSetup class 中的主要问题。 class 依赖于 运行 的播放应用程序,而它不应该。经过一些重构后,它现在可以正常工作了。

要进行路由测试,您需要在测试中有 Application 上下文。
测试应该从

开始
"deliver a correct svg file" in new WithApplication {
....
}

您可以在 Play's page about functional tests 查看更多相关信息。