gpt4 book ai didi

scala - 戏剧中的 Specs2 测试给了我“找不到 org.specs2.main.CommandLineAsResult 类型的证据参数的隐式值

转载 作者:行者123 更新时间:2023-12-01 08:26:04 31 4
gpt4 key购买 nike

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

但是,当我尝试运行 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

我在其他应用程序中也有类似的测试,但看起来新版本的规范增加了很多对 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

最佳答案

我认为您使用了错误的 WithApplication 导入。
使用这个:

import play.api.test.WithApplication

关于scala - 戏剧中的 Specs2 测试给了我“找不到 org.specs2.main.CommandLineAsResult 类型的证据参数的隐式值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34869237/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com