【问题标题】:How can I resolve conflicting actor systems while testing akka-http and akka actors at the same spec file?在同一个规范文件中测试 akka-http 和 akka 演员时,如何解决冲突的演员系统?
【发布时间】:2020-07-09 17:58:25
【问题描述】:

我有一个使用 akka-http 定义的Route,它使用内部的参与者发送消息。 我的路线如下所示:

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

编译测试失败,因为存在冲突的参与者系统:

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 相关,但我们可能能够以不同的方式解决我想要在这种情况下实现的目标。

【问题讨论】:

  • ScalaTestWithActorTestKitScalatestRouteTest 不兼容。如果你真的不需要ScalaTestWithActorTestKit,也许你可以删除它?

标签: scala testing akka akka-http akka-actor


【解决方案1】:

我在这里花了一点时间来打字。对于仍在寻找的人,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 的第一条评论

也许只是一个文档补充表明您不需要使用 Akka Typed 中的 ActorTestKit 并且可以使用 TestProbes 例如https://gist.github.com/chbatey/964b80adc2cd124fa4bf4624927b5be0

val probe = TestProbe[Ping]() > val probe = testKit.createTestProbe[Ping]()

【讨论】:

    猜你喜欢
    • 2014-03-09
    • 2015-07-24
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 2013-11-29
    • 2019-05-10
    相关资源
    最近更新 更多