只是编写单元测试以确保演员在某些条件下关闭,所以我有一个测试:
val tddTestActor = TestActorRef[MyActor](Props(classOf[MyActor], "param1"))
tddTestActor ! someMessage
tddTestActor.isTerminated shouldBe true
我正在接受警告 isTerminated 已弃用。提示建议我使用 context.watch()但是在单元测试中我没有父actor或任何上下文可供观看。
验证 tddTestActor 关闭的bext方法是什么?
答案 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)
}
}
}