3

我从这里遵循喷雾手册。所以我收集了非常简单的测试

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)

我在这里错过了一些重要的概念吗?

4

1 回答 1

4

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

使用 spray-testkit,您需要sealRoute(...)按照此处的说明包装您的路线:http: //spray.io/documentation/1.2.2/spray-testkit/#sealing-routes

尝试这个:

Post("/ato/v1/orders/updateStatus") ~> sealRoute(new AtoImportServiceApi().route) ~> check { // ...
于 2015-05-15T13:57:51.933 回答