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

标签 scala playframework sbt integration-testing specifications

我正在尝试在 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")
    }
  }
}

一切都非常简单。问题是在 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")

最佳答案

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 兼容的依赖项。此外,您无需在所有依赖项中重复 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 。看起来 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-mocks 的代码来了解这一点, play2-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
)

关于scala - 在 play.api.test 中找不到 Play Framework 2.4 WithApplication 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35944390/

相关文章:

scala - 我可以在 Scala 中将一个元素生成或映射为多个元素吗?

scala - Scala Trait 定义中 -= 和 += 的含义

scala - 带有 Play 框架的 ImplicitParam 中的 Swagger 数据类型模型

jquery - Play Framework jquery webjar 集成不起作用

scala - SqlContext 不是包 org.apache.spark.sql 的成员

mysql - 从 Apache Spark 访问带有文本列的 MySql 表

scala - Slick未插入但没有错误

java - 玩!框架 - 访问 "private"文件

scala - SBT 远程调试在 intellij 中有效,但在执行测试时无效

scala - 在 SBT 中启动 Scala 应用程序进行集成测试