我正在尝试使用scalatest测试POST喷射路线,这需要强制参数adId。并且无法使其发挥作用。我的代码如下
import akka.actor._
import akka.event.LoggingReceive
import akka.testkit.{TestProbe}
import com.ss.rg.service.ad.AdImporterServiceActor.{GetImportStatus, StatusOfImport}
import org.scalatest.{MustMatchers, WordSpecLike}
import spray.http.{StatusCodes, MediaTypes}
import spray.testkit.ScalatestRouteTest
class AdServiceApiTest extends WordSpecLike with MustMatchers with ScalatestRouteTest{
"AdService REST api " must{
"POST for import witout mandatory parameters should fail with " in{
val p = TestProbe()
val addressServiceMock = system.actorOf(Props(classOf[AdServiceActorMock],p.ref))
Post("/service/ad/import") ~> new AdServiceApi(addressServiceMock).route ~>check{
handled must be(false)
status must be (StatusCodes.BadRequest)
}
}
}
测试失败,但原因不同
Request was rejected with List(MissingQueryParamRejection(adId))
org.scalatest.exceptions.TestFailedException: Request was rejected with List(MissingQueryParamRejection(adId))
at spray.testkit.ScalatestInterface$class.failTest(ScalatestInterface.scala:25)
at com.ss.rg.api.ad.AdServiceApiTest.failTest(AdServiceApiTest.scala:19)
at spray.testkit.RouteResultComponent$RouteResult$$anonfun$response$1$$anonfun$apply$1.apply(RouteResultComponent.scala:97)
at spray.testkit.RouteResultComponent$RouteResult$$anonfun$response$1$$anonfun$apply$1.apply(RouteResultComponent.scala:95)
at scala.Option.foreach(Option.scala:236)
at spray.testkit.RouteResultComponent$RouteResult$$anonfun$response$1.apply(RouteResultComponent.scala:94)
...
似乎甚至没有检查过状态。 还没有完全清除的第二件事是如何在spray-testkit中实际设置adId参数?一种方法是通过设置标题,但我不会感到惊讶,存在更好的方法。
有人对spray-testkit评论更有经验吗?
THX
答案 0 :(得分:1)
没有状态 - 请求的路由rejected。您可以rejection
访问拒绝,并声明它是您期望的类型。如果你想检查浏览器实际会看到什么,你应该使用默认的handleRejections
(它隐含地可用)在RejectionHandler
指令中包装路线然后你就可以了查看您期望的状态代码。在这种情况下,handled
将以true
形式出现(因为包装的路由将处理请求 - 通过返回带有失败状态代码和错误消息的响应)。