【问题标题】:How to override guice modules in Playframework unit tests using ScalaTest如何使用 ScalaTest 在 Playframework 单元测试中覆盖 guice 模块
【发布时间】:2015-11-28 13:56:32
【问题描述】:

我想在 PlayFramework 中为我的控制器编写功能测试。为此,我想模拟一些类的实现。

我在这里找到了如何使用 spec2 的好例子:http://www.innovaedge.com/2015/07/01/how-to-use-mocks-in-injected-objects-with-guiceplayscala/

但我正在使用带有 OneAppPerSuite 特征的 scala 测试,该特征使用 FakeApplication。以下是文档: https://www.playframework.com/documentation/2.4.x/ScalaFunctionalTestingWithScalaTest

问题是我找不到拦截GuiceApplicationBuilder 并使用模拟实现覆盖一些绑定的方法。

这里是来自play.api.testFakeApplication 实现:

case class FakeApplication(
  override val path: java.io.File = new java.io.File("."),
  override val classloader: ClassLoader = classOf[FakeApplication].getClassLoader,
  additionalPlugins: Seq[String] = Nil,
  withoutPlugins: Seq[String] = Nil,
  additionalConfiguration: Map[String, _ <: Any] = Map.empty,
  withGlobal: Option[play.api.GlobalSettings] = None,
  withRoutes: PartialFunction[(String, String), Handler] = PartialFunction.empty) extends Application {

private val app: Application = new GuiceApplicationBuilder()
  .in(Environment(path, classloader, Mode.Test))
  .global(withGlobal.orNull)
  .configure(additionalConfiguration)
  .bindings(
    bind[FakePluginsConfig] to FakePluginsConfig(additionalPlugins, withoutPlugins),
    bind[FakeRouterConfig] to FakeRouterConfig(withRoutes))
  .overrides(
    bind[Plugins].toProvider[FakePluginsProvider],
    bind[Router].toProvider[FakeRouterProvider])
  .build

所以我无法拦截到GuiceApplicationBuilder 并覆盖绑定。

我是 playframework 的新手,如果问题看起来有点傻,我很抱歉。 谢谢!

【问题讨论】:

    标签: scala unit-testing playframework guice scalatest


    【解决方案1】:

    您可能使用的是旧版本的 ScalaTestPlus,它不支持使用 Application 覆盖 FakeApplication。在Play docs(Play 2.4) 中,库版本是“1.4.0-M3”,但应该是“1.4.0”。

    【讨论】:

      猜你喜欢
      • 2015-01-12
      • 2017-12-14
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多