Play 2.3 FakeApplication 模式没有在测试中设置?

Play 2.3 FakeApplication mode not setting in test?

我正在使用 play 2.3.8,并且在我的 GlobalSettings 中有一些配置会根据应用程序的模式而改变。所以我有这样的东西:

object Global extends GlobalSettings {
    override def onLoadConfig(config: Configuration, path: java.io.File, classloader: ClassLoader, mode: Mode.Mode) = {
        println(mode)
        val customConfig = //Based on mode.*
        config ++ configuration ++ Configuration(ConfigFactory.parseMap(customConfig))
    }
}

然后我尝试编写测试以确保此行为有效:

class MyTest extends PlaySpec {

    val testApp = FakeApplication(
        additionalConfiguration = Map(
            //SomeSettings And Stuff
            "logger.application" -> "WARN",
            "logger.root" -> "WARN"
        )
    )

    val devApp = new FakeApplication(
        additionalConfiguration = Map(
            //SomeSettings And Stuff
            "logger.application" -> "WARN",
            "logger.root" -> "WARN"
        )
    ) {
        override val mode = Mode.Dev
    }

    val prodApp = new FakeApplication(
        additionalConfiguration = Map(
            //SomeSettings And Stuff
            "logger.application" -> "WARN",
            "logger.root" -> "WARN"
        )
    ) {
        override val mode = Mode.Prod
    }

    "ThisNonWorkingTestOfMine" must {

        "when running application in test mode have config.thing = false" in running(testApp) {
            assertResult(Mode.Test)(testApp.mode)
            assertResult(false)(testApp.configuration.getBoolean("config.thing").get)
        }

        "when running application in dev mode have config.thing = false" in running(devApp) {
            assertResult(Mode.Dev)(devApp.mode)
            assertResult(false)(devApp.configuration.getBoolean("config.thing").get)
        }

        "when running application in prod mode have config.thing = true" in running(prodApp) {
            assertResult(Mode.Prod)(prodApp.mode)
            assertResult(true)(prodApp.configuration.getBoolean("config.thing").get)
        }
    }

}

当我 运行 这些测试时,我从手边看到了一些奇怪的东西 println:

Test
null
null
[info] MyTest:
[info] ThisNonWorkingTestOfMine
[info] play - Starting application default Akka system.
[info] play - Shutdown application default Akka system.
[info] - must when running application in test mode have config.thing = false
[info] play - Application started (Dev)
[info] - must when running application in dev mode have config.thing = false
[info] play - Application started (Prod)
[info] - must when running application in prod mode have config.thing = true *** FAILED ***
[info]   Expected true, but got false (MyTest.scala:64)
[info] ScalaTest

如何在Play 2.3中正确设置FakeApplication的模式?我现在拥有它的方式基于 Mastering Play 的页面,但显然这不是使用 onLoadConfig 的方式

编辑: 我也在试验 OneAppPerTest 并在 newAppForTest 方法中创建 FakeApplication 但它仍然表现得很奇怪,与上面的方法一样为 null。这真的很奇怪,因为如果我在制作 FakeApplication 时在 additionalConfiguration 地图中设置一个随机 属性 像 "foo" -> "bar" 然后尝试从 config.getString 中读取它我的 Global 对象,它被记录为 None,即使我在测试本身中做 app.configuration.getString 它显示 bar。感觉这里有某种脱节。如果我使用 FakeApplication.apply 方法而不是 new FakeApplication

,我不会为模式得到 null

所以我认为这与 the way that FakeApplication sets the mode to Mode.Test via override 有关,因为如果我复制 FakeApplication class 并删除该行并创建我自己的 class 版本,让我设置我没有问题的模式。换句话说,在我的测试包中,我声明了以下 class:

package play.api.test

import play.api.mvc._
import play.api.libs.json.JsValue
import scala.concurrent.Future
import xml.NodeSeq
import play.core.Router
import scala.runtime.AbstractPartialFunction
import play.api.libs.Files.TemporaryFile
import play.api.{ Application, WithDefaultConfiguration, WithDefaultGlobal, WithDefaultPlugins }

case class FakeModeApplication(
    override val path: java.io.File = new java.io.File("."),
    override val classloader: ClassLoader = classOf[FakeModeApplication].getClassLoader,
    val additionalPlugins: Seq[String] = Nil,
    val withoutPlugins: Seq[String] = Nil,
    val additionalConfiguration: Map[String, _ <: Any] = Map.empty,
    val withGlobal: Option[play.api.GlobalSettings] = None,
    val withRoutes: PartialFunction[(String, String), Handler] = PartialFunction.empty,
    val mode: play.api.Mode.Value
) extends {
    override val sources = None
} with Application with WithDefaultConfiguration with WithDefaultGlobal with WithDefaultPlugins {
    override def pluginClasses = {
        additionalPlugins ++ super.pluginClasses.diff(withoutPlugins)
    }

    override def configuration = {
        super.configuration ++ play.api.Configuration.from(additionalConfiguration)
    }

    override lazy val global = withGlobal.getOrElse(super.global)

    override lazy val routes: Option[Router.Routes] = {
        val parentRoutes = loadRoutes
        Some(new Router.Routes() {
            def documentation = parentRoutes.map(_.documentation).getOrElse(Nil)
            // Use withRoutes first, then delegate to the parentRoutes if no route is defined
            val routes = new AbstractPartialFunction[RequestHeader, Handler] {
                override def applyOrElse[A <: RequestHeader, B >: Handler](rh: A, default: A => B) =
                    withRoutes.applyOrElse((rh.method, rh.path), (_: (String, String)) => default(rh))
                def isDefinedAt(rh: RequestHeader) = withRoutes.isDefinedAt((rh.method, rh.path))
            } orElse new AbstractPartialFunction[RequestHeader, Handler] {
                override def applyOrElse[A <: RequestHeader, B >: Handler](rh: A, default: A => B) =
                    parentRoutes.map(_.routes.applyOrElse(rh, default)).getOrElse(default(rh))
                def isDefinedAt(x: RequestHeader) = parentRoutes.map(_.routes.isDefinedAt(x)).getOrElse(false)
            }
            def setPrefix(prefix: String) {
                parentRoutes.foreach(_.setPrefix(prefix))
            }
            def prefix = parentRoutes.map(_.prefix).getOrElse("")
        })
    }
}

然后在我的测试中我可以像这样使用它:

val devApp = new FakeModeApplication(
    additionalConfiguration = Map(
        //SomeSettings And Stuff
        "logger.application" -> "WARN",
        "logger.root" -> "WARN"
    ), mode = Mode.Dev
)

然后模式值按照我设置的方式显示,而不是 null

我post将此作为答案,因为它确实解决了我面临的问题,但我不了解为什么像这样制作 FakeApplication 时的新关键字:new FakeApplication() { override mode ... } 导致模式在 GlobalSettings 的 onLoadConfig 方法中作为 null 通过。这感觉像是一种 hack 而不是解决方案,如果任何对此有足够了解的人可以 post 一个不涉及复制完整 FakeApplication class 和更改一行的解决方案,我将不胜感激。