【问题标题】:Akka Actor isTerminated deprecatedAkka Actor isTerminated 已弃用
【发布时间】:2016-03-29 22:04:57
【问题描述】:

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

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

我收到一条警告,isTerminated 已弃用。提示建议我使用 context.watch() 但是在单元测试中我没有父演员或任何上下文可供观看。

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

【问题讨论】:

  • cmbaxter 的好答案,使用 TestProbe() 观察演员,然后使用 expectTerminated() 进行测试

标签: scala akka akka-testkit


【解决方案1】:

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

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

然后,将 specs2 与 akka 的 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)
    }
  }

}

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 2019-08-20
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    相关资源
    最近更新 更多