Scalatest FunSuite 和 Akka Actor
我想编写一个使用 Akka Actor 并从 sbt 运行的 ScalaTest 测试套件。当我尝试这样做时:
class Tests extends FunSuite with BeforeAndAfterAll {
override protected def beforeAll() {
class Actor1 extends Actor {
protected def receive = {
case 1 => println("One")
}
}
val sys = ActorSystem("my")
val a = sys.actorOf(Props[Actor1], "plain_actor")
a ! 1
sys.shutdown()
}
}
然后 sbt test
,我得到了
[ERROR] [01/22/2012 12:49:50.329] [default-dispatcher10] [akka://my/user/plain_actor] error while creating actor
但是当我在通常的主类而不是 FunSuite 中编写相同的代码,并通过 sbt run
运行它时>,所有作品。这两种情况有什么区别,如何让 Akka Actor 在测试套件中正确运行?
I want to write a ScalaTest test suite that uses Akka actors and runs from sbt. When I try to do this:
class Tests extends FunSuite with BeforeAndAfterAll {
override protected def beforeAll() {
class Actor1 extends Actor {
protected def receive = {
case 1 => println("One")
}
}
val sys = ActorSystem("my")
val a = sys.actorOf(Props[Actor1], "plain_actor")
a ! 1
sys.shutdown()
}
}
and then sbt test
, I get
[ERROR] [01/22/2012 12:49:50.329] [default-dispatcher10] [akka://my/user/plain_actor] error while creating actor
But when I write the same code in a usual main class instead of a FunSuite, and run it by sbt run
, all works. What is the difference between these two cases and how do I get Akka actors run correctly in a test suite?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 Prop[X],则 X 需要使用 newInstance 进行实例化,但如果您在方法内部声明它则不是这样。
在包或对象中定义 Actor 类或使用 Props(new Actor1)
If you use Prop[X] then X needs to be instantiable using newInstance, which it isn't if you declare it internally in the method.
Define the Actor class either in a package or in an object or use Props(new Actor1)