Play Framework 2.5 Test ApplicationLifecycle Guice Specs2 setup

时间:2016-11-21 13:19:26

标签: scala guice playframework-2.5

我正在尝试使用play2-reactivemongo运行一些功能测试。我会尽量做到具体,但如果遗漏了什么,请告诉我。

我的依赖关系在这里

libraryDependencies ++= Seq(
  cache,
  "org.reactivemongo" %% "play2-reactivemongo" %  "0.12.0",
  "com.mohiva" %% "play-silhouette" % "4.0.0",
  "com.mohiva" %% "play-silhouette-testkit" % "4.0.0" % "test",
  specs2 % Test
)

MongoUserDao.scala

import play.modules.reactivemongo._
import play.modules.reactivemongo.json._
import reactivemongo.play.json.collection.JSONCollection

class MongoUserDao @Inject() (val reactiveMongoApi: ReactiveMongoApi) extends UserDao {
  val usersFuture = reactiveMongoApi.database.map(_.collection[JSONCollection]("users"))
...
}

DaoSpecResources.scala

trait DaoSpecResources {

  val timeout = DurationInt(10).seconds

  val fakeApp = new GuiceApplicationBuilder()
    .in(Mode.Test)
    .configure(
      "play.modules.enabled" -> List("play.modules.reactivemongo.ReactiveMongoModule"),
      "mongodb.uri" -> "mongodb://localhost:27017/test"
    )
    .build

  val reactiveMongoApi = fakeApp.injector.instanceOf[ReactiveMongoApi]
  ...
}

当我尝试运行测试时出现此错误

[error] cannot create an instance for class daos.UserDaoSpec
[error]   caused by com.google.inject.CreationException: Unable to create injector, see the following errors:
[error]
[error] 1) No implementation for play.api.inject.ApplicationLifecycle was bound.
[error]   while locating play.api.inject.ApplicationLifecycle
[error]     for parameter 1 at services.ApplicationTimer.<init>(ApplicationTimer.scala:24)
[error]   at Module.configure(Module.scala:23) (via modules: com.google.inject.util.Modules$OverrideModule -> Module)
[error]
[error] 1 error

1 个答案:

答案 0 :(得分:2)

这是由app/services/ApplicationTimer.scala引起的,这取决于ApplicationLifecycle,但您尚未将任何实施约束到ApplicationLifecycleApplicationTimer是每个新Play项目中包含的演示。如果你不需要它,你应该完全删除它,否则如果在测试模式下运行,至少要禁用它。请参阅disabling modulesoverriding modules

解决方案

但是,由于DefaultReactiveMongoApi还取决于ApplicationLifecycle,因此您需要提供对ApplicationLifecycle的实施的绑定。最简单的方法:

import play.api.inject.{ ApplicationLifecycle, DefaultApplicationLifecycle }
import play.api.inject.bind

trait DaoSpecResources {

  val timeout = DurationInt(10).seconds

  val fakeApp = new GuiceApplicationBuilder()
    .in(Mode.Test)
    .configure(
      "play.modules.enabled" -> List("play.modules.reactivemongo.ReactiveMongoModule"),
      "mongodb.uri" -> "mongodb://localhost:27017/test"
    )
    .bindings(bind[ApplicationLifecycle].to[DefaultApplicationLifecycle])
    .build

  val reactiveMongoApi = fakeApp.injector.instanceOf[ReactiveMongoApi]

  val lifecycle = fakeApp.injector.instanceOf[DefaultApplicationLifecycle]
  def stopApp = lifecycle.stop()

}

(添加了5行:导入,bindingslifecyclestopApp

然后,在您的测试规范中,最后添加step(stopApp),如下所示:

class FooSpec extends PlaySpecification with DaoSpecResources {

    // Your examples...

    step(stopApp)

}

替代解决方案

就个人而言,我会创建一个延伸SpecificationPlaySpecification的专业特征,可以自动设置和删除所有内容,例如in this example from specs2 documentation

trait PlayWithMongoSpecification extends PlaySpecification {

  val timeout = DurationInt(10).seconds

  val fakeApp = new GuiceApplicationBuilder()
    .in(Mode.Test)
    .configure(
      "play.modules.enabled" -> List("play.modules.reactivemongo.ReactiveMongoModule"),
      "mongodb.uri" -> "mongodb://localhost:27017/test"
    )
    .bindings(bind[ApplicationLifecycle].to[DefaultApplicationLifecycle])
    .build

  val reactiveMongoApi = fakeApp.injector.instanceOf[ReactiveMongoApi]

  val lifecycle = fakeApp.injector.instanceOf[DefaultApplicationLifecycle]
  def stopApp = lifecycle.stop()

  override def map(fs: =>Fragments) = fs ^ step(stopApp)

}

class FooSpec extends PlayWithMongoSpecification {

    // Your examples...

}

您可以考虑将reactiveMongoApi设为lazy val