We have an actor over whom we write unit tests, and as part of the tests, we want to argue that certain messages are sent to another actor in a specific order. In our unit tests, the message-receiving actor is represented by Akka TestProbe, which is introduced into the actor being tested when it is created.
There is no problem to claim that the messages were sent for a test study, however we tried our best to find a way to say that they were sent in the correct order (we could not find suitable methods for this in the documentation ). Any ideas on how we achieve this?
Below is a minimal implementation that emphasizes the problem.
Implementation
case class Message(message: String)
case class ForwardedMessage(message: String)
class ForwardingActor(forwardTo: ActorRef) extends Actor {
def receive = {
case Message(message) =>
forwardTo ! ForwardedMessage(message)
}
}
Unit test
class ForwardMessagesInOrderTest extends TestKit(ActorSystem("testSystem"))
with WordSpecLike
with MustMatchers {
"A forwarding actor" must {
val forwardingReceiver = TestProbe()
val forwardingActor = system.actorOf(Props(new ForwardingActor(forwardingReceiver.ref)))
"forward messages in the order they are received" in {
forwardingActor ! Message("First message")
forwardingActor ! Message("Second message")
forwardingReceiver.expectMsgAllOf(
ForwardedMessage("Second message"),
ForwardedMessage("First message"))
}
}
}