0

我正在尝试使用需要强制参数 adId 的 scalatest 测试 POST 喷雾路线。并且不能让它工作。我的代码如下

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 评论更有经验吗?

谢谢

4

1 回答 1

1

没有状态 - 路由拒绝了请求。您可以访问拒绝rejection并断言它是您期望的类型。如果您想检查浏览器实际看到的内容,您应该handleRejections使用默认值(它隐式可用)将路由包装在指令中RejectionHandler,然后您将能够看到您期望的状态代码。在那种情况下handled会出现true(因为包装的路由将处理请求 - 通过返回带有失败状态码和错误消息的响应)。

于 2015-03-16T15:52:51.923 回答