Akka Actor被终止弃用

时间:2016-03-29 22:04:58

标签: scala akka akka-testkit

只是编写单元测试以确保演员在某些条件下关闭,所以我有一个测试:

    val tddTestActor = TestActorRef[MyActor](Props(classOf[MyActor], "param1"))
    tddTestActor ! someMessage
    tddTestActor.isTerminated shouldBe true

我正在接受警告 isTerminated 已弃用。提示建议我使用 context.watch()但是在单元测试中我没有父actor或任何上下文可供观看。

验证 tddTestActor 关闭的bext方法是什么?

1 个答案:

答案 0 :(得分:2)

我同意观看是完成这项工作的最佳方式。当我测试停止行为时,我通常会使用TestProbe作为观察者来检查我的演员。假设我有一个非常简单的Actor定义如下:

class ActorToTest extends Actor{
  def receive = {
    case "foo" => 
      sender() ! "bar"
      context stop self
  }
}

然后,将specs2与akka' s TestKit结合使用,我可以像这样测试停止行为:

class StopTest extends TestKit(ActorSystem()) with SpecificationLike with ImplicitSender{

  trait scoping extends Scope {
    val watcher = TestProbe()
    val actor = TestActorRef[ActorToTest]
    watcher.watch(actor)
  }

  "Sending the test actor a foo message" should{
    "respond with 'bar' and then stop" in new scoping{
      actor ! "foo"
      expectMsg("bar")
      watcher.expectTerminated(actor)
    }
  }

}