2016-01-19 32 views
3

我想在Play2/Scala中编写一个简单的REST API的测试用例来发送/接收JSON我的测试如下所示:Specs2测试内戏剧给了我“无法找到类型org.specs2.main.CommandLineAsResult的证据参数的隐式值

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":"[email protected]", | 
          "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") 
    } 
    ... 

然而,当我尝试运行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 

我在其他应用程序类似的测试,但它看起来像新版本的规格增加了对期货和其他东西的大量支持帽子使之前的教程无效。我在斯卡拉2.11.6,激活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 

回答

6

我想你使用了错误的WithApplication进口。 使用这一个:

import play.api.test.WithApplication 
+0

我刚刚向我的大学展示我的google-foo很好,谢谢你! – mulya

2

测试用例的最后一行应该是断言/评估声明。

例如在失败的测试用例方法的最后一个}之前放置语句false must beEqualTo(true)并再次运行。

相关问题