scala - 玩!斯卡拉 : How to disable actor modules loaded at the start of the application when testing?

标签 scala playframework akka guice

我在游戏开始时加载了一些 Actor ! 2.5应用程序如下:

class Module extends AbstractModule with AkkaGuiceSupport with ScalaModule {
  override def configure() = {
    bindActor[MainSupervisor]("main-supervisor")
  }
}

问题是,当我运行测试时,我从加载的参与者(以及整个集群和远程系统)收到很多日志(和不必要的调用),例如

[INFO ] a.r.Remoting: Starting remoting 
[INFO ] a.r.Remoting: Remoting started; listening on addresses :[akka.tcp://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="660716160a0f0507120f090826575451485648564857" rel="noreferrer noopener nofollow">[email protected]</a>:41496] 
[INFO ] a.r.Remoting: Remoting now listens on addresses: [akka.tcp://<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="127362627e7b7173667b7d7c522320253c223c223c23" rel="noreferrer noopener nofollow">[email protected]</a>:41496] 

例如,我测试了一个不需要任何 Actor 的类,但我找不到任何方法来禁用它们(甚至更好地禁用整个 Actor 系统)。

我尝试过的是:

lazy val appWithoutActorsBuilder = new GuiceApplicationBuilder()
  .disable[ActorSystem]
  .disable[MainSupervisor]
  .build()
lazy val injectorWithoutActors = appWithoutActorsBuilder.injector
lazy val wSClientWithoutActors = injectorWithoutActors.instanceOf[WSClient]
lazy val ec = scala.concurrent.ExecutionContext.Implicits.global
lazy val facebookAPI = new FacebookAPI(wSClientWithoutActors, ec)

但是当我测试 FacebookAPI 方法时(例如 facebookAPI.method(...) MustBe ...),我仍然看到来自 Akka 的日志。我该怎么做才能避免这种情况?

最佳答案

您必须禁用您的模块而不是 Actor 。因此,考虑到您有此类:

package com.acme.modules

class Module extends AbstractModule with AkkaGuiceSupport with ScalaModule {
  override def configure() = {
    bindActor[MainSupervisor]("main-supervisor")
  }
}

并且您正在您的 conf/application.conf 中注册它,如下所示:

play.modules.enabled += "com.acme.modules.Module"

然后,在测试时,您必须执行以下操作:

lazy val appWithoutActorsBuilder = new GuiceApplicationBuilder()
  .disable[com.acme.modules.Module]
  .build()

关于scala - 玩!斯卡拉 : How to disable actor modules loaded at the start of the application when testing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37816805/

相关文章:

scala - 如何将 scala 案例类声明为 Scalaz 的 Semigroup 的实例?

java - 如何更改 Play Framework 2 "test"设置以显示完整的堆栈跟踪?

java - 无法从 GET 请求中获取 JSON

scala - Gitlab Auto Devops中的regex用于scoverage-gradle插件

scala - 点阅读器 monad scala

JAVA - 实例化 Scala 案例类

spring-boot - WRK 基准测试 : Please explain results

java - Akka:如何关闭未终止的 Actor

unit-testing - Akka 中的 TestKit、TestActorRef 和 TestProbe 是什么?

scala - Play Framework 2.3 在 Controller 中处理 Future