使用Testkit EventListeners使用ScalaTest进行Akka Actor测试
有人能解释一下为什么失败吗?这是Akka文档示例的一个直接复制到规范中。我遗漏了什么使用Testkit EventListeners使用ScalaTest进行Akka Actor测试,scala,akka,scalatest,Scala,Akka,Scalatest,有人能解释一下为什么失败吗?这是Akka文档示例的一个直接复制到规范中。我遗漏了什么 import akka.actor.{ActorSystem, Props, ActorKilledException, Kill} import akka.testkit.{ TestKit, EventFilter} import org.scalatest.{FlatSpecLike, BeforeAndAfterAll} import com.typesafe.config.ConfigFactory
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
}
}
}
在每次测试中保持关闭
,而不是覆盖,毕竟
起作用,但当然需要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)
}
}
我的身材.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"
)
由于您在
之后
aval
对其进行了构造评估,并且在运行测试之前将关闭actor系统。将其更改为一种方法(def
),它将工作。因为您制作了之后
aval
它将在构建时进行评估,并且在运行测试之前,将关闭actor系统。将它更改为一个方法(def
),它就会工作。谢谢你,约翰-我想我一定是在做傻事!真的很感谢你花时间回答谢谢你约翰-我想我一定是在做傻事!非常感谢您抽出时间回答