【问题标题】:Route TestKit in asynchronous ScalaTest在异步 ScalaTest 中路由 TestKit
【发布时间】:2018-01-22 02:50:35
【问题描述】:

我正在将一些 Akka HTTP 代码的同步 ScalaTest 测试切换到 AsyncFunSpec。是否也有一种简单的方法可以使 Akka TestKit 测试异步?我说的是这样的代码:

Get("/test") ~> testRoute ~> check {
    responseAs[String] shouldEqual "Fragments of imagination"
}

我基本上需要的是check 的一个版本,它返回Future 而不是调用await。或者一个辅助函数,它将 Get("/test") 之类的 HttpRequest 转换为 RequestContext,以便我可以将路由应用到它。

【问题讨论】:

标签: scala asynchronous scalatest akka-http akka-testkit


【解决方案1】:

我最终使用了这样的东西:

import akka.http.scaladsl.client.RequestBuilding.Get
import akka.http.scaladsl.server.Route

val handler = Route.asyncHandler(testRoute)

for {
  response <- handler(Get("/test"))
  strict <- response.entity.toStrict
  res <- strict.toString shouldEqual "Fragments of imagination"
} yield res

【讨论】:

    猜你喜欢
    • 2019-12-10
    • 2012-08-15
    • 1970-01-01
    • 2019-01-04
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 2014-09-18
    相关资源
    最近更新 更多