用Akka对一名副作用演员进行单元测试

时间:2015-01-29 02:55:46

标签: scala unit-testing akka

给定一个具有以下接收方法定义的actor:

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
  }
}

和一个测试类(用Scalacheck编写。),定义为:

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)
  }
}

请注意,ActorSpec混合使用以下特征:

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

它还包括来自Akka TestKit的implicitSender特征 我最终得到以下错误:

  

[INFO] [01/26/2015 13:49:27.287] [testsystem-akka.actor.default-dispatcher-4] [akka:// testsystem / user / EmailActor]消息[akka.actor.PoisonPill $ ]从演员[akka:// testsystem / user / EmailActor#1885968450]到演员[akka:// testsystem / user / EmailActor#1885968450]未送达。 [1]遇到死信。可以使用配置设置关闭或调整此日志记录' akka.log-dead-letters'和' akka.log-dead-letters-during-shutdown'。

如何设置测试,以便在测试消息接收时实际通过?

0 个答案:

没有答案