在play中的Specs2测试给我"找不到org.specs2.main.CommandLineAsResult类型的证据参数的隐含值

djs*_*dog 3 scala playframework specs2 playframework-2.0

我正在尝试为Play2/Scala中的一个发送/接收JSON的简单REST API编写测试用例.我的测试如下所示:

import org.junit.runner.RunWith
import org.specs2.matcher.JsonMatchers
import org.specs2.mutable._
import org.specs2.runner.JUnitRunner
import play.api.libs.json.{Json, JsArray, JsValue}
import play.api.test.Helpers._
import play.api.test._
import play.test.WithApplication

/**
  * Add your spec here.
  * You can mock out a whole application including requests, plugins etc.
  * For more information, consult the wiki.
  */

@RunWith(classOf[JUnitRunner])
class APIv1Spec extends Specification with JsonMatchers {

  val registrationJson = Json.parse("""{"device":"576b9cdc-d3c3-4a3d-9689-8cd2a3e84442", |
                             "firstName":"", "lastName":"Johnny", "email":"justjohnny@test.com", |
                             "pass":"myPassword", "acceptTermsOfService":true}
                                """)

  def dropJsonElement(json : JsValue, element : String) = (json \ element).get match {
    case JsArray(items) => util.dropAt(items, 1)
  }

  def invalidRegistrationData(remove : String) = {
    dropJsonElement(registrationJson,remove)
  }

  "API" should {

    "Return Error on missing first name" in new WithApplication {

      val result= route(
        FakeRequest(
          POST,
          "/api/v1/security/register",
          FakeHeaders(Seq( ("Content-Type", "application/json") )),
          invalidRegistrationData("firstName").toString()
        )
      ).get

      status(result) must equalTo(BAD_REQUEST)
      contentType(result) must beSome("application/json")
    }
    ...
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试运行时sbt test,我收到以下错误:

Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=384M; support was removed in 8.0
[info] Loading project definition from /home/cassius/brentspace/esalestracker/project
[info] Set current project to eSalesTracker (in build file:/home/cassius/brentspace/esalestracker/)
[info] Compiling 3 Scala sources to /home/cassius/brentspace/esalestracker/target/scala-2.11/test-classes...
[error] /home/cassius/brentspace/esalestracker/test/APIv1Spec.scala:34: could not find implicit value for evidence parameter of type org.specs2.main.CommandLineAsResult[play.test.WithApplication{val result: scala.concurrent.Future[play.api.mvc.Result]}]
[error]     "Return Error on missing first name" in new WithApplication {
[error]                                          ^
[error] one error found
[error] (test:compileIncremental) Compilation failed
[error] Total time: 3 s, completed 18/01/2016 9:30:42 PM
Run Code Online (Sandbox Code Playgroud)

我在其他应用程序中有类似的测试,但看起来新版本的规范为Futures和其他使以前的教程无效的东西增加了很多支持.我在Scala 2.11.6,Activator 1.3.6上,我的build.sbt如下所示:

name := """eSalesTracker"""

version := "1.0-SNAPSHOT"

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

scalaVersion := "2.11.6"

libraryDependencies ++= Seq(
  jdbc,
  cache,
  ws,
  "com.typesafe.slick" %% "slick"      % "3.1.0",
  "org.postgresql" % "postgresql" % "9.4-1206-jdbc42",
  "org.slf4j" % "slf4j-api" % "1.7.13",
  "ch.qos.logback" % "logback-classic" % "1.1.3",
  "ch.qos.logback" % "logback-core" % "1.1.3",
  evolutions,
  specs2 % Test,
  "org.specs2" %% "specs2-matcher-extra" % "3.7" % Test
)

resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"
resolvers += Resolver.url("Typesafe Ivy releases", url("https://repo.typesafe.com/typesafe/ivy-releases"))(Resolver.ivyStylePatterns)

// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
routesGenerator := InjectedRoutesGenerator
Run Code Online (Sandbox Code Playgroud)

Tom*_*mer 7

我认为你使用错误的WithApplication导入.使用这个:

import play.api.test.WithApplication
Run Code Online (Sandbox Code Playgroud)