Testing: FakeApplication ignoring additionalConfiguration

耗尽温柔 提交于 2019-12-11 18:07:16

问题


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_ENABLEDis 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!