喷拒不转换为状态码?

Spray rejections is not converted to status code?

我正在遵循 here 的喷雾手册。所以我收集了非常简单的测试

class AtoImportServiceApiTest extends WordSpecLike with MustMatchers with ScalatestRouteTest {

  "AtoImportService" must {
    "return HTTP status 401 Unauhorized when accessing withou basic auth" in {
      Post("/ato/v1/orders/updateStatus") ~>new AtoImportServiceApi().route ~> check {
        handled must be(false)
        rejections must have size 1
        status === StatusCodes.Unauthorized
      }
    }
  }
}

我正在调用包含授权指令的路由。所以我希望拒绝将转换为 HTTP 状态代码。但这并没有发生在这里,测试失败了。

Request was rejected with List(AuthenticationFailedRejection(CredentialsMissing,List(WWW-Authenticate: Basic realm="bd ato import api")))
ScalaTestFailureLocation: spray.testkit.RouteTest$class at (RouteTest.scala:74)
org.scalatest.exceptions.TestFailedException: Request was rejected with List(AuthenticationFailedRejection(CredentialsMissing,List(WWW-Authenticate: Basic realm="bd ato import api")))
    at spray.testkit.ScalatestInterface$class.failTest(ScalatestInterface.scala:25)

我是不是漏掉了一些重要的概念?

您需要 "seal the route" 才能查看实际状态代码。密封路由意味着默认的拒绝和异常处理程序用于处理任何迄今为止未处理的拒绝和异常。这在您的服务中使用 runRoute 时会自动完成,但在测试套件中不会自动完成以允许您直接检查拒绝。

使用 spray-testkit,你需要用 sealRoute(...) 包装你的路线,如下所述:http://spray.io/documentation/1.2.2/spray-testkit/#sealing-routes

试试这个:

Post("/ato/v1/orders/updateStatus") ~> sealRoute(new AtoImportServiceApi().route) ~> check { // ...