mercredi 28 janvier 2015

Unit testing a side effecting actor with Akka

Given an actor with the following receive method definition:



def receive = {
case SendEmail ⇒ {
mailer.setup
val body = prepareMessage(config.getString("source"), config.getString("template"))
mailer.send(body, System.getenv("mailuser"), List(""), None, "stuff")
listener.foreach(_ ! SendEmail)
log.info("hey") //This line does get executed.
self ! PoisonPill
}
}


and a test class (Written with Scalacheck.) with the definition:



class EmailActorSpec extends ActorSpec {
behavior of "An EmailActor"
it should "send an email" in {
val mailMock = new TestMailAccessor
val props = Props(new EmailActor(Some(testActor), mailMock))
val emailer = system.actorOf(props, "EmailActor")
emailer ! SendEmail
val result = expectMsgType[SendEmail](100 millis)
expectMsg(result)
}
}


Note that ActorSpec mixes in the following trait:



trait StopSystemAfterAll extends BeforeAndAfterAll {
this: TestKit with Suite ⇒
override protected def afterAll() {
super.afterAll()
system.shutdown()
}


I end up with the following error:



[INFO] [01/26/2015 13:49:27.287] [testsystem-akka.actor.default-dispatcher-4] [akka://testsystem/user/EmailActor] Message [akka.actor.PoisonPill$] from Actor[akka://testsystem/user/EmailActor#1885968450] to Actor[akka://testsystem/user/EmailActor#1885968450] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.



How do I setup the tests so that when testing for message reception they actually pass?


Aucun commentaire:

Enregistrer un commentaire