使用 Testkit EventListeners 使用 ScalaTest 进行 Akka Actor 测试

Akka Actor testing with ScalaTest using Testkit EventListeners

有人可以解释为什么失败吗?它是 Akka 文档示例 http://doc.akka.io/docs/akka/snapshot/scala/testing.html#Expecting_Log_Messages 到规范的直接副本。我错过了什么?

import akka.actor.{ActorSystem, Props, ActorKilledException, Kill}
import akka.testkit.{ TestKit, EventFilter}
import org.scalatest.{FlatSpecLike, BeforeAndAfterAll}
import com.typesafe.config.ConfigFactory

class MySpec() 
  extends TestKit(ActorSystem("testsystem", ConfigFactory.parseString(
    """akka.loggers = ["akka.testkit.TestEventListener"]""")))
  with FlatSpecLike with BeforeAndAfterAll {

  override val afterAll = shutdown(system)
  //afterAll executes without waiting for the EventFilter.intercept, resulting in 
  //  java.lang.IllegalStateException: cannot create children while terminating or terminated
  //  at akka.actor.dungeon.Children$class.makeChild(Children.scala:213)...

  behavior of "MyActor"

  it should "throw an exception when killed" in {
    val actor = system.actorOf(Props.empty)
    EventFilter[ActorKilledException](occurrences = 1) intercept {
      actor ! Kill
    }
  } 
}

在每个测试中保留 shutdown 而不是覆盖 afterAll 是可行的,但当然需要 OneInstancePerTest

it should "throw an exception when killed" in {
  try {
    val actor = system.actorOf(Props.empty)
    EventFilter[ActorKilledException](occurrences = 1) intercept {
      actor ! Kill
    }
  } finally {
    shutdown(system)
  }
} 

我的build.sbt

lazy val root = (project in file(".")).
  settings(
    name := "TestkitAfterall",
    version := "1.0",
    scalaVersion := "2.11.7"
  )

lazy val akkaVersion = "2.4.0"

libraryDependencies ++=  Seq(
  "com.typesafe.akka" %% "akka-actor" % akkaVersion,
  "com.typesafe.akka" %% "akka-testkit" % akkaVersion,
  "org.scalatest" % "scalatest_2.11" % "2.2.5"
)

由于您制作了 afterAll 一个 val,因此在构建时对其进行评估,并且在测试 运行 之前关闭 actor 系统。将其更改为一种方法 (def),它将起作用。