Scalatest FunSuite 和 Akka Actor

发布于 2024-12-28 02:39:07 字数 752 浏览 4 评论 0原文

我想编写一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彻夜缠绵 2025-01-04 02:39:07

如果您使用 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文