测试:FakeApplication忽略了额外的配置 [英] Testing: FakeApplication ignoring additionalConfiguration

查看:122
本文介绍了测试:FakeApplication忽略了额外的配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在带有Scala项目的Play 2.0上添加测试:

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")
      }
    }
}

Config.IS_WEB_ENABLED定义为:

object Config {

  lazy val IS_WEB_ENABLED = Play.configuration.getBoolean("enable.webInterface").getOrElse(false)

}

您可以看到测试,我尝试将enable.webInterface的配置设置覆盖为false,因为application.conf文件默认将其设置为true.但是FakeApplication没有获得新的配置值.

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?

推荐答案

使用def而不是lazy val,也许您之前使用过Config.IS_WEB_ENABLED,并且它被初始化为true,并且只返回相同的结果,因为它是一个val.

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)

}

这篇关于测试:FakeApplication忽略了额外的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆