Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
646 views
in Technique[技术] by (71.8m points)

scala - Testing Akka-http and getting error: No implicit arguments of type: Emptiness[Seq[Rejection]]

I have a test using Akka-http which has the error in Intellij No implicit arguments of type: Emptiness[Seq[Rejection]]. However the code compiles using sbt compile and the test passes using sbt test. Even when I run the test from the Intellij it passes. IntelliJ shows the error on the line rejections should not be empty or when I use rejections.should(not).be(empty).

First I had a problem using ~> implicit operator and I fixed by adding implicit val timeout: RouteTestTimeout = RouteTestTimeout(2 seconds). Otherwise I had to explicit use request.~>(gameRoutes)(TildeArrow.injectIntoRoute), as it is saying in this question. I am using the following version on 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
)

the test:

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
      }
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...