Scala 记录发送到Akka TestKit TestProbe的所有消息

Scala 记录发送到Akka TestKit TestProbe的所有消息,scala,akka,testkit,Scala,Akka,Testkit,我正在尝试记录TestKit TestProbe接收到的所有消息,这被证明有点困难。我知道文档中有一节说应该结合使用LogginReceive块使用debug.receive选项。然而,当我不能控制参与者的实现时,这就不起作用了 我唯一的想法是对akka.testkit.TestActor进行子类化,使用LoggingReceive,然后使用子类testkit来创建我的TestActor子类的实例,但这不起作用,因为大多数功能都是akka命名空间的私有功能(我想这是有原因的)。对不起,一开始你的

我正在尝试记录TestKit TestProbe接收到的所有消息,这被证明有点困难。我知道文档中有一节说应该结合使用
LogginReceive
块使用
debug.receive
选项。然而,当我不能控制参与者的实现时,这就不起作用了


我唯一的想法是对
akka.testkit.TestActor
进行子类化,使用
LoggingReceive
,然后使用子类
testkit
来创建我的
TestActor
子类的实例,但这不起作用,因为大多数功能都是
akka
命名空间的私有功能(我想这是有原因的)。

对不起,一开始你的问题有点错,所以我的方法是这样的

创建一个包装器参与者,用于记录消息:

class LoggingActor(fac: => Actor) extends Actor {

  val underlying = context.system.actorOf(Props(fac))

  def receive = {
    LoggingReceive {
      case x ⇒ underlying.tell(x, sender)
    }
  }
}
然后创建您的
TestActorRef
,将您的参与者包装在
LoggingActor
中:

  val echo = TestActorRef(new LoggingActor(new FooActor))

  echo ! "hello world"
有一个(可能令人惊讶)简单的答案:

probe.setAutoPilot(new TestActor.AutoPilot {
  def run(sender: ActorRef, msg: Any) = {
    log.debug("whatever")
    this
  }
})

使用Ronald的答案,我写了这篇文章,以更简单的方式定义我的探测器:

object LoggingTestProbe {
  def apply()(implicit system: ActorSystem) : TestProbe = {
    val probe = TestProbe()
    probe.setAutoPilot(new TestActor.AutoPilot {
      def run(sender: ActorRef, msg: Any) = {
        val other = sender.path
        val me = probe.ref.path
        system.log.debug(s"$me received $msg from $other")
        this
      }
    })
    probe
  }
}
有了这个,我使用
LoggingTestProbe()
而不是
TestProbe()
来定义我的探测


我是Scala新手,所以这可能不是最优的,但对我来说效果很好。

可能更简单,但有点神秘。什么是自动驾驶仪?文档对此没有说明。自动驾驶仪现在在文档中,仅供参考。这不是最简单的概念,但上面的代码非常简单,并且实现了这个技巧(我只记录sender&msg值,而不是“随便什么”)显然是字面上的)。