我正在使用ScalaTest和Akka TestKit编写单元测试和集成测试,针对一个我已经编写的演员,使其仅向另一个演员发送消息而不改变任何内部状态。比如这个例子:
class MyActor extends Actor {
val anotherActor = context.actorOf(Props[AnotherActor])
def receive: Receive = {
case MyMessage => anotherActor ! AnotherMessage
}
}
我想编写一个测试,确认anotherActor
在MyActor
处理MyMessage
的结果下,处理了AnotherMessage
。经典的示例是使用TestActorRef
来获取基础actor,并检查应该在消息接收时受影响的某些内部状态,如下所示:
val testActorRef = TestActorRef(new MyActor)
"MyActor" should "change some internal state when it receives MyMessage" in {
testActorRef ! MyMessage
testActorRef.underlyingActor.someState shouldBe "altered"
}
但在我的情况下,我不关心这种状态。实际上,我想要避免持有任何这样的状态。TestProbe
也不是我正在寻找的,因为您仍然需要向正在测试的actor注册TestProbe.ref
。就大部分而言,我查看了Akka文档中关于测试的所有示例 (http://doc.akka.io/docs/akka/snapshot/scala/testing.html),但没有找到任何合适的内容。