简体   繁体   English

玩! Scala:如何在测试时禁用在应用程序启动时加载的actor模块?

[英]Play! Scala: How to disable actor modules loaded at the start of the application when testing?

I load some actors at the beginning of my Play! 我在Play的开头加载了一些演员! 2.5 application like this: 2.5应用程序是这样的:

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

The problem is that when I run my tests I got a lot a logs (and unnecessary calls) from the loaded actor (and the entire cluster and remote system) like 问题是,当我运行测试时,我从加载的actor(以及整个集群和远程系统)获得了很多日志(和不必要的调用),例如

[INFO ] a.r.Remoting: Starting remoting 
[INFO ] a.r.Remoting: Remoting started; listening on addresses :[akka.tcp://application@127.0.0.1:41496] 
[INFO ] a.r.Remoting: Remoting now listens on addresses: [akka.tcp://application@127.0.0.1:41496] 

I have, for instance, a class that I test where I don't need any actor, but I don't find any way to disable them (or even better the entire actor system). 例如,我有一个类,可以在不需要任何actor的地方进行测试,但是我找不到任何禁用它们的方法(甚至更好的是禁用整个actor系统)。

What I have tried is: 我试过的是:

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)

But when I test the FacebookAPI methods (eg facebookAPI.method(...) mustBe ... ) I still see the logs from Akka. 但是当我测试FacebookAPI方法(例如facebookAPI.method(...) mustBe ... )时,我仍然看到来自Akka的日志。 What can I do in order to avoid it? 为了避免这种情况该怎么办?

You have to disable your module instead of your actor. 您必须禁用模块而不是actor。 So, considering that you have this class: 因此,考虑到您拥有此类:

package com.acme.modules

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

And that you are registering it at your conf/application.conf like this: 并且您正在conf/application.conf中注册它,如下所示:

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

Then, at your tests, your have to do the following: 然后,在测试时,您必须执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM