Embedding multiple tests inside an akka

I'm using akka-http for the first time - my usual web framework of choice is http4s - and I'm having trouble getting the way I usually write endpoint unit tests to work with the route testing provided by akka-http-testkit.

Generally, I use ScalaTest (FreeSpec flavour) in order to set up an endpoint call and then run several separate tests on the response. For akka-http-testkit, this would look like:

import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.testkit.ScalatestRouteTest
import org.scalatest.{FreeSpec, Matchers}

final class Test extends FreeSpec with ScalatestRouteTest with Matchers {

  val route: Route = path("hello") {
    get {
      complete("world")
    }
  }

  "A GET request to the hello endpoint" - {
    Get("/hello") ~> route ~> check {
      "should return status 200" in {
        status should be(StatusCodes.OK)
      }

      "should return a response body of 'world'" in {
        responseAs[String] should be("world")
      }

      //more tests go here
    }
  }
}

This errors with

java.lang.RuntimeException: This value is only available inside of a `check` construct!

The problem is the nested tests inside the check block - for some reason, values like status and responseAs are only available top-level within that block. I can avoid the error by saving the values I'm interested in to local variables top-level, but that's awkward and capable of crashing the test framework if eg the response parsing fails.

Is there a way around this, without putting all my assertions into a single test or performing a new request for each one?

链接地址: http://www.djcxy.com/p/65694.html

上一篇: xzgrep和其他压缩模式匹配工具

下一篇: 在一个阿卡内嵌入多个测试