Scala 测试中,自定义喷洒拒绝处理程序工作不正常
我正在使用Scala 测试中,自定义喷洒拒绝处理程序工作不正常,scala,akka,spray,specs2,Scala,Akka,Spray,Specs2,我正在使用spray构建一些JSON HTTP服务,在测试RejectionHandler时遇到了一些问题。如果我启动运行命令sbt run的应用程序并发出请求,则RejectionHandler会按预期处理MalformedRequestContentRejection,但即使在密封路由的情况下运行测试时,我也会收到IllegalArgumentException。另一方面,MethodRejection工作正常。JSON验证使用require 下一个示例基于带有POST端点和新测试的spra
spray
构建一些JSON HTTP服务,在测试RejectionHandler
时遇到了一些问题。如果我启动运行命令sbt run
的应用程序并发出请求,则RejectionHandler
会按预期处理MalformedRequestContentRejection
,但即使在密封路由的情况下运行测试时,我也会收到IllegalArgumentException
。另一方面,MethodRejection
工作正常。JSON验证使用require
下一个示例基于带有POST端点和新测试的spray-can 1.3\u scala-2.11上的spray template
分支。我用整个例子做了一个叉子
注意,反序列化JSON使用了case-clase
,验证使用了require
方法,并声明了隐式RejectionHandler
package com.example
import akka.actor.Actor
import spray.routing._
import spray.http._
import StatusCodes._
import MediaTypes._
import spray.httpx.SprayJsonSupport._
class MyServiceActor extends Actor with MyService {
def actorRefFactory = context
def receive = runRoute(myRoute)
}
case class SomeReq(field: String) {
require(!field.isEmpty, "field can not be empty")
}
object SomeReq {
import spray.json.DefaultJsonProtocol._
implicit val newUserReqFormat = jsonFormat1(SomeReq.apply)
}
trait MyService extends HttpService {
implicit val myRejectionHandler = RejectionHandler {
case MethodRejection(supported) :: _ => complete(MethodNotAllowed, supported.value)
case MalformedRequestContentRejection(message, cause) :: _ => complete(BadRequest, "requirement failed: field can not be empty")
}
val myRoute =
pathEndOrSingleSlash {
post {
entity(as[SomeReq]) { req =>
{
complete(Created, req)
}
}
}
}
}
这是使用spray testkit
实施的测试。最后一个需要一个BadRequest
,但测试失败,出现IllegarArgumentException
package com.example
import org.specs2.mutable.Specification
import spray.testkit.Specs2RouteTest
import spray.http._
import StatusCodes._
import spray.httpx.SprayJsonSupport._
class MyServiceSpec extends Specification with Specs2RouteTest with MyService {
def actorRefFactory = system
"MyService" should {
"leave GET requests to other paths unhandled" in {
Get("/kermit") ~> myRoute ~> check {
handled must beFalse
}
}
"return a MethodNotAllowed error for PUT requests to the root path" in {
Put() ~> sealRoute(myRoute) ~> check {
status should be(MethodNotAllowed)
responseAs[String] === "POST"
}
}
"return Created for POST requests to the root path" in {
Post("/", new SomeReq("text")) ~> myRoute ~> check {
status should be(Created)
responseAs[SomeReq] === new SomeReq("text")
}
}
/* Failed test. Throws IllegalArgumentException */
"return BadRequest for POST requests to the root path without field" in {
Post("/", new SomeReq("")) ~> sealRoute(myRoute) ~> check {
status should be(BadRequest)
responseAs[String] === "requirement failed: field can not be empty"
}
}
}
}
我错过了什么
提前谢谢 您的SomeReq
类正在Post(“/”),new SomeReq(“”)请求生成器中被急切地实例化,并且require
方法在类
实例化后立即被调用
要解决此问题,请尝试使用以下方法:
import spray.json.DefaultJsonProtocol._
Post("/", JsObject("field" → JsString(""))) ~> sealRoute(myRoute) ~> check {
status should be(BadRequest)
responseAs[String] === "requirement failed: field can not be empty"
}
谢谢工作起来很有魅力。