在play.api.test中找不到Play Framework 2.4 WithApplication调用

时间:2016-03-11 15:44:23

标签: scala playframework sbt integration-testing specifications

我正在尝试在播放框架2.4中简单测试一条路线,并按照指南进行操作:https://www.playframework.com/documentation/2.4.x/ScalaFunctionalTestingWithSpecs2(测试路由器)

这里是代码

package routesAndController

import org.specs2.mutable._
import org.specs2.runner._
import org.junit.runner._

import play.api.test._
import play.api.test.Helpers._

/**
  * Created by root on 3/11/16.
  */
@RunWith(classOf[JUnitRunner])
class AnalysisEntryPointTest extends Specification {


  "the AnalysisEntryPoint" should {
    "where the route must be /DomoticRoomServer/Analysis with 200" in new WithApplication {
        val result = route(FakeRequest(GET, "/domoticRoom/analysis")).get

        status(result) must equalTo(OK)
        contentType(result) must beSome.which(_ == "text/html")
    }
  }
}

一切都很直接。问题是在play.api.test包中找不到类'WithApplication',而在play.test中找不到。 我试图在api.test中使用该对象,但specs2给出了错误:

[error] /home/benkio/projects/DomoticRoom/Server/test/routesAndController/AnalysisEntryPointTest.scala:19: could not find implicit value for evidence parameter of type org.specs2.execute.AsResult[play.test.WithApplication{val result: scala.concurrent.Future[play.api.mvc.Result]}]
[error]     "where the route must be /DomoticRoomServer/Analysis with 200" in new WithApplication() {
[error]                                                                    ^
[error] one error found
[error] (test:compileIncremental) Compilation failed

有什么建议吗?

这里是build.sbt:

import play.routes.compiler.InjectedRoutesGenerator
import play.sbt.PlayScala

name := """Domotic Room Server"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayScala)

scalaVersion := "2.11.7"

resolvers ++= Seq(
  "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases",
  "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
  "Millhouse Bintray"  at "http://dl.bintray.com/themillhousegroup/maven"
)

libraryDependencies ++= Seq(
  "com.typesafe.play" %% "play-cache" % "2.4.6",
  "org.specs2" %% "specs2-core" % "3.6" % "test",
  "org.specs2" %% "specs2-junit" % "3.6" % "test",
  "org.specs2" %% "specs2-scalacheck" % "3.6" % "test",
  "org.reactivemongo" %% "play2-reactivemongo" % "0.11.9",
  "com.themillhousegroup" %% "play2-reactivemongo-mocks" % "0.11.9_0.4.26"
)

// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
routesGenerator := InjectedRoutesGenerator
scalacOptions in Test ++= Seq("-Yrangepos")

fork in run := true

这是我的项目/ plugin.sbt:

// The Typesafe repository
resolvers += "Typesafe repository" at "https://repo.typesafe.com/typesafe/releases/"

// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.6")

1 个答案:

答案 0 :(得分:2)

Play有一个快捷方式来声明测试依赖项,包括它自己的包。添加specs2和Play测试类的正确方法是:

libraryDependencies ++= Seq(
    "com.typesafe.play" %% "play-cache" % "2.4.6",
    "org.reactivemongo" %% "play2-reactivemongo" % "0.11.9",
    "com.themillhousegroup" %% "play2-reactivemongo-mocks" % "0.11.9_0.4.26",
    specs2 % Test
)

这是documented here。还有一个使用缓存的快捷方式,以及documented here。所以你的依赖项应该像这样声明:

libraryDependencies ++= Seq(
    "org.reactivemongo" %% "play2-reactivemongo" % "0.11.9",
    "com.themillhousegroup" %% "play2-reactivemongo-mocks" % "0.11.9_0.4.26",
    cache,
    specs2 % Test
)

此处的优点是您不需要跟踪与Play兼容的依赖项。此外,您不需要在project/plugins.sbt文件的所有依赖项上重复Play版本。

当然,您仍然可以根据需要覆盖和添加任何其他依赖项。您正在为每个实例添加scalacheck:

libraryDependencies ++= Seq(
    "org.reactivemongo" %% "play2-reactivemongo" % "0.11.9",
    "com.themillhousegroup" %% "play2-reactivemongo-mocks" % "0.11.9_0.4.26",
    cache,
    specs2 % Test,
    "org.specs2" %% "specs2-scalacheck" % "3.6" % Test
)

讨论后编辑:

欢迎来到Dependency Hell。看起来play2-reactivemongoplay2-reactivemongo-mocks正在添加一个非常旧的specs2依赖项。您可以使用sbt-dependency-graph并运行sbt dependencyTree来查看。以下是the complete output以及相关部分:

[info]   +-com.themillhousegroup:play2-reactivemongo-mocks_2.11:0.11.9_0.4.27 [S]
[info]   | +-org.reactivemongo:play2-reactivemongo_2.11:0.11.10 [S]
[info]   | +-org.specs2:specs2_2.11:2.3.13 [S]

您还可以看到play2-reactivemongo-mocksplay2-reactivemongoPlay Framework 2.4.6的代码。这些是specs2不兼容的版本,而且sbt无法驱逐旧版本,因为项目都添加了不同的specs2包(请参阅play添加特定依赖项与play2-reactivemongo-mocks相比)。

换句话说,看起来play2-reactivemongo-mocks提供的测试支持与Play提供的测试支持不兼容。您可以打开问题或提交拉取请求来解决此问题,但是需要新版本的play2-reactivemongo-mocks。

可能的解决方案

从play2-reactive依赖项中排除specs2:

libraryDependencies ++= Seq(
  "org.reactivemongo" %% "play2-reactivemongo" % "0.11.10" exclude("org.specs2", "*"),
  "com.themillhousegroup" %% "play2-reactivemongo-mocks" % "0.11.9_0.4.27" exclude("org.specs2", "*"),
  cache,
  specs2 % Test,
  "org.specs2" %% "specs2-scalacheck" % "3.6" % Test
)