我想知道测试演员是否已在阿卡(Akka)中停下来的规范方法是什么。这是我目前工作方式的一个例子。我担心我要使其复杂化。

import akka.actor.{Terminated, Actor, Props, ActorSystem}
import akka.testkit.TestProbe

class MyActor extends Actor {
  import MyActor._
  override def receive: Receive = {
    case Stop => context.stop(self)
  }
}

object MyActor {
  def props = Props(new MyActor)
  case object Stop
}


object MyActorSpec {

  val system = ActorSystem()
  val myActor = system.actorOf(MyActor.props)
  val testProbe = TestProbe()

  case object MyActorStopped

  val watcher = system.actorOf(Props(new Actor {
    context.watch(myActor)
    override def receive: Actor.Receive = {
      case Terminated(`myActor`) => testProbe.ref ! MyActorStopped
    }
  }))

  myActor ! MyActor.Stop
  testProbe.expectMsg(MyActorStopped)
}

最佳答案

您可以摆脱单独的观察者演员,而直接从testProbe演员观察目标演员:

val testProbe = TestProbe()
testProbe watch myActor
myActor ! MyActor.Stop
testProbe.expectTerminated(myActor)


有关Akka测试文档的相关部分,请参见here

10-06 04:05