我有一个使用 akka-http 定义的 Route
,它使用内部的 actor 来发送消息。
我的路线如下所示:
path("entity") {
post {
entity(as[Enrtity]) {
entity =>
val result: Future[Message] = mainActor.ask {
ref: akka.actor.typed.ActorRef[Message] =>
Message(
entity = entity,
replyRef = ref
)
}
complete("OK")
}
}
}
我的测试规范:
class APITest
extends ScalaTestWithActorTestKit(ManualTime.config)
with ScalatestRouteTest
with AnyWordSpecLike {
val manualTime: ManualTime = ManualTime()
// my tests here ...
}
编译测试失败,因为存在冲突的 actor 系统:
class APITest inherits conflicting members:
[error] implicit def system: akka.actor.typed.ActorSystem[Nothing] (defined in class ActorTestKitBase) and
[error] implicit val system: akka.actor.ActorSystem (defined in trait RouteTest)
重写 actor 系统也无济于事,因为继承的 actor 系统既有类型的,也有非类型的。 我怎样才能轻松解决这个问题?
更新:
这与 conflicting inherited members 有关使用不同的类型,但我们可能能够以不同的方式解决我想要在这种情况下实现的目标。
最佳答案
我在转向打字时在这里花了一点时间。对于仍在寻找的人,https://developer.lightbend.com/guides/akka-http-quickstart-scala/testing-routes.html 有一个很好的提示
// the Akka HTTP route testkit does not yet support a typed actor system (https://github.com/akka/akka-http/issues/2036)
// so we have to adapt for now
lazy val testKit = ActorTestKit()
implicit def typedSystem = testKit.system
override def createActorSystem(): akka.actor.ActorSystem =
testKit.system.classicSystem
查看 https://github.com/akka/akka-http/issues/2036 上的第一条评论它指出
Perhaps just a docs addition to show that you don't need to use the ActorTestKit from Akka Typed and can just use TestProbes e.g. https://gist.github.com/chbatey/964b80adc2cd124fa4bf4624927b5be0
或 val probe = TestProbe[Ping]()
> val probe = testKit.createTestProbe[Ping]()
关于scala - 在同一规范文件中测试 akka-http 和 akka actor 时,如何解决冲突的 actor 系统?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60912372/