2
votes

I am switching synchronous ScalaTest tests of some Akka HTTP code to AsyncFunSpec. Is there a simple way to make Akka TestKit tests asynchronous, as well? I am talking about code like:

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

What I would basically need is a version of check which returns a Future instead of calling await. Or a helper function which converts a HttpRequest like Get("/test") into a RequestContext so that I can apply the route to it.

1
What people seem to be doing is globally set a timeout stretch factor so that tests don't timeout: doc.akka.io/docs/akka/current/scala/…js.

1 Answers

1
votes

I ended up using something like this:

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