akka-expect is an expect mimic to facilitate easy testing of Akka actors. Following are a few examples, please see ExpectActorSpec for the full spec.
In order to demonstrate the features of akka-expect we need the following two actors:
class EchoService extends Actor {
override def receive = {
case msg => self reply msg
}
}
class DiscardService extends Actor {
override def receive = {
case msg =>
}
}
The expectActor is created using
val expectActor = actorOf[ExpectActor].start
In order to avoid specifying the expectActor as a sender every time, we declare it implicit:
implicit val senderOption = Option(expectActor)
"ExpectActor" should "enable synchronous assertion of specific messages" in {
echoService ! "hello"
expectActor ? "hello"
}
it should "enable explicit assertion of specific messages that are not expected" in {
echoService ! "hola"
expectActor ? noneOf("hello")
expectActor ? "hola"
}
it should "enable explicit assertion of no messages of any kind" in {
discardService ! "hello"
expectActor ? nothing
}
it should "enable assertion of multiple messages received in specific order" in {
echoService ! "hello"
echoService ! "world"
expectActor ? inOrder("hello", "world")
}
it should "enable assertion of multiple messages received in any order" in {
echoService ! "hello"
echoService ! "world"
expectActor ? anyOrder("world", "hello")
}
it should "enable assertion of messages of a specific class" in {
echoService ! "hello"
expectActor ? anyOf(classOf[String])
}