问题
I'm trying to add a test on a Play 2.0 w/Scala project:
"Application" should {
"return 404 on the index Action if web is disabled " in {
running(FakeApplication(additionalConfiguration = Map(("enable.webInterface" -> "false")) )) {
Config.IS_WEB_ENABLED must beFalse
val result = controllers.Application.index()(FakeRequest())
status(result) must equalTo(NOT_FOUND)
contentType(result) must beSome("text/html")
charset(result) must beSome("utf-8")
}
}
}
The value Config.IS_WEB_ENABLED
is defined as:
object Config {
lazy val IS_WEB_ENABLED = Play.configuration.getBoolean("enable.webInterface").getOrElse(false)
}
As you can see the test I try to override the configuration setting for enable.webInterface
to false as the application.conf
file has it set to true by default. But FakeApplication is not getting the new configuration value.
Any idea about what I'm missing there?
回答1:
Use def
instead of lazy val
, maybe you used Config.IS_WEB_ENABLED before and it was initialized with true and returns only the same result because it's a val.
object Config {
def IS_WEB_ENABLED = Play.configuration.getBoolean("enable.webInterface").getOrElse(false)
}
回答2:
I suspect that Play.configuration
does not take the fake application into account.
So try with this (using current
):
object Config {
lazy val IS_WEB_ENABLED = Play.current.configuration.getBoolean("enable.webInterface").getOrElse(false)
}
回答3:
THe Map
passed in FakeApplication
is a Map[String, String]
and you are requesting a boolean from the configuration. I would suspect the value not being picked up because it does not have the right type. What happens if you try
Play.configuration.getString("enable.webInterface")
instead ?
来源:https://stackoverflow.com/questions/13333718/testing-fakeapplication-ignoring-additionalconfiguration