首页 > 解决方案 > 测试 Akka-http 并出现错误:没有类型的隐式参数:Emptiness[Seq[Rejection]]

问题描述

我有一个使用 Akka-http 的测试,它在 Intellij 中有错误No implicit arguments of type: Emptiness[Seq[Rejection]]。然而,代码使用编译sbt compile并且测试通过使用sbt test. 即使我从 Intellij 运行测试,它也通过了。IntelliJ 在线显示错误rejections should not be empty或当我使用rejections.should(not).be(empty).

首先,我在使用~>隐式运算符时遇到了问题,我通过添加implicit val timeout: RouteTestTimeout = RouteTestTimeout(2 seconds). 否则我必须明确使用request.~>(gameRoutes)(TildeArrow.injectIntoRoute),正如在这个问题中所说的那样。我正在使用以下版本build.sbt

scalaVersion := "2.12.7"
val akkaVersion = "2.6.10"
val scalaTestVersion = "3.2.0"
lazy val akkaHttpVersion = "10.2.2"
libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % akkaVersion,
  "com.typesafe.akka" %% "akka-testkit" % akkaVersion,
  "com.typesafe.akka" %% "akka-http" % akkaHttpVersion,
  "com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion,
  "com.typesafe.akka" %% "akka-http-testkit" % akkaHttpVersion,
  "com.typesafe.akka" %% "akka-http2-support" % akkaHttpVersion
)

考试:

import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.MethodRejection
import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest}
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import scala.concurrent.duration._

class MarshallingJSONSpec
  extends AnyWordSpec
    with Matchers
    with ScalatestRouteTest
    with PlayerJsonProtocol {

  import MarshallingJSON._

  implicit val timeout: RouteTestTimeout = RouteTestTimeout(2 seconds)

  "A Game area map backend" should {
    "not accept other methods than POST and GET and DELETE" in {
      Put("/api/player") ~> gameRoutes ~> check {
        rejections should not be empty // IntteliJ SAYS THERE IS AN ERROR HERE
        // rejections.should(not).be(empty) // SAME

        val methodRejections = rejections.collect {
          case rejection: MethodRejection => rejection
        }
        methodRejections.length shouldBe 3
      }
    }
  }
}

标签: scalaintellij-ideaakkascalatestakka-http

解决方案


推荐阅读