我同意观看是完成这项工作的最佳方式。当我测试停止行为时,我通常会使用 aTestProbe
作为观察者来检查我的被测演员。假设我有一个非常简单的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)
}
}
}