在 play.api.test 中找不到 Play Framework 2.4 WithApplication 调用
Play Framework 2.4 WithApplication call not found in play.api.test
我正在尝试在 play framework 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")
}
}
}
一切都很简单。问题是 Class 'WithApplication' 不在 play.api.test 包中,而是在 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
这是我的 project/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")
Play 有一个快捷方式来声明测试依赖项,包括它自己的包。添加 specs2 和 Play test 类 的正确方法是:
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. There is also a shortcut to use cache, as also 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
文件中即可。
当然,您仍然可以根据需要覆盖和添加任何其他依赖项。您正在为每个实例添加 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. Looks like that play2-reactivemongo
and play2-reactivemongo-mocks
are adding a very old specs2 dependency. You can see that by using sbt-dependency-graph and running sbt dependencyTree
. Here is 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-mocks, play2-reactivemongo and Play 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
)
我正在尝试在 play framework 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")
}
}
}
一切都很简单。问题是 Class 'WithApplication' 不在 play.api.test 包中,而是在 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
这是我的 project/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")
Play 有一个快捷方式来声明测试依赖项,包括它自己的包。添加 specs2 和 Play test 类 的正确方法是:
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. There is also a shortcut to use cache, as also 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
文件中即可。
当然,您仍然可以根据需要覆盖和添加任何其他依赖项。您正在为每个实例添加 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. Looks like that play2-reactivemongo
and play2-reactivemongo-mocks
are adding a very old specs2 dependency. You can see that by using sbt-dependency-graph and running sbt dependencyTree
. Here is 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-mocks, play2-reactivemongo and Play 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
)