如何在Play 2.1中指定其他配置文件进行测试 [英] How can I specify a different config file for testing in Play 2.1

查看:85
本文介绍了如何在Play 2.1中指定其他配置文件进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为多个测试环境(生产,暂存,开发)定义不同的数据库连接.阅读文章"

I would like to define different database connections for multiple test environments(Production, Staging, Development). After reading the post "How do I specify a config file with sbt 0.12.2 for sbt test?" it seems that it was possible in earlier versions of Play, by using the follwing SBT setting:

val main = play.Project(appName, appVersion, appDependencies).settings(
    javaOptions in Test += "-Dconfig.file=conf/test.conf"
)

但是,如果我在Build.scala中使用此设置,则会出现以下错误:

But if I use this setting in my Build.scala, I get the following error:

not found: value javaOptions

所以我的问题是,如何为不同的测试环境定义不同的连接?

So my question is, how can I define different connections for different test environments?

修改: 可能的解决方法是在测试过程中覆盖默认设置.这可以通过环境变量来完成.

A possible workaround would be to override the default setting during testing. This can be done with a environment variable.

object Config {
  var defaultConfig = Map(
    "db.default.user" -> "user",
    "db.default.pass" -> "************"
  )

  def additionalConfiguration(): Map[String, _] = sys.env.getOrElse("PLAY_TEST_SCOPE", "") match {
    case "development" => {
      defaultConfig += "db.default.url" -> "jdbc:mysql://host:3306/development"
      defaultConfig
    }
    case "staging" => {
      defaultConfig += "db.default.url" -> "jdbc:mysql://host:3306/staging"
      defaultConfig
    }
    case "production" => {
      defaultConfig += "db.default.url" -> "jdbc:mysql://host:3306/production"
      defaultConfig
    }
    case _ => {
      throw new Exception("Environment variable `PLAY_TEST_SCOPE` isn't defined")
    }
  }
}

然后使用此配置运行伪造的应用程序.

And then running a fake application with this configuration.

FakeApplication(additionalConfiguration = Config.additionalConfiguration())

推荐答案

javaOptions包含在确保在Build.scala文件中使用正确的导入:

Make sure that you use the proper import in your Build.scala file:

import Keys._

这篇关于如何在Play 2.1中指定其他配置文件进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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