I'm using ScalaTest with the Akka TestKit to write unit and integration tests for an actor I've coded to simply send a message to another actor without mutating any internal state. Take this for example:
class MyActor extends Actor {
val anotherActor = context.actorOf(Props[AnotherActor])
def receive: Receive = {
case MyMessage => anotherActor ! AnotherMessage
}
}
I want to write a test that confirms that anotherActor
processed AnotherMessage
as a consequence of MyActor
processing MyMessage
. The classic example is to use TestActorRef
to get at the underlying actor and check for some internal state that should have been affected upon message receipt like so:
val testActorRef = TestActorRef(new MyActor)
"MyActor" should "change some internal state when it receives MyMessage" in {
testActorRef ! MyMessage
testActorRef.underlyingActor.someState shouldBe "altered"
}
But in my case I don't care about such state. In fact, I want to avoid holding any such state. TestProbe
wasn't quite what I was looking for either since you still have to register aTestProbe.ref
with the actor under test. For the most part, I've looked at all the examples in the Akka documentation on testing (http://doc.akka.io/docs/akka/snapshot/scala/testing.html) but haven't found anything suitable.