scala - 测试 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
}
}
}
}
解决方案
推荐阅读
- swift - 由于未声明的类型,如何将代码转换为 Swift 4?
- azure-application-insights - 监控 Azure Application Insight 数据摄取
- promise - axios拦截器:如何在onFulfill中抛出错误?
- c - 是否有可能在 C 中导致可以在更高级别的编程语言中尝试捕获的类似异常的状态?
- javascript - 如何将结果显示到小数点后 2 位?
- python - 使用 CUDA 训练期间的运行时错误:图上的边缘条件卷积
- google-apps-script - 如何从 Google 电子表格中的 Google Apps 脚本自动更新“站点地图”功能?
- css - IE 11 中带有媒体查询的嵌套 CSS 自定义属性(CSS 变量)?
- f#-interactive - 没有 F# Interactive:“无法生成 FSI,请确保它在 PATH 中”
- python - 保存 tensorboard tf.summary.image 而不进行颜色转换