使用测试配置播放2.0 FakeApplication设置 [英] Play 2.0 FakeApplication setup with test configuration

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

问题描述

我有一个specs2测试,该测试使用FakeApplication和嵌入式mongodb数据库.

I have a specs2 test which uses a FakeApplication and an embedded mongodb database.

def inMemoryMongoDatabase(name: String = "default"): Map[String, String] = {
    val dbname: String = "play-test-" + scala.util.Random.nextInt
    Map(
        ("mongodb." + name + ".db" -> dbname),
        ("mongodb." + name + ".port" -> EmbeddedMongoTestPort.toString))
}

override def around[T <% Result](t: => T) = {
    running(FakeApplication(additionalConfiguration = inMemoryMongoDatabase(), additionalPlugins = Seq("se.radley.plugin.salat.SalatPlugin"))) {
        t // execute t inside a http session
    }
}

FakeApplication使用conf目录中的默认application.conf配置以及为每个测试创建的测试数据库的其他配置.
在我们设置mongodb复制集之前,这一直是有效的发现.现在application.conf包含此复制集的配置

The FakeApplication uses the default application.conf configuration in the conf directory and additional configuration for the test databases that are created for each test.
This was working find until we setup a mongodb replicat set. Now the application.conf contains configuration for this replicat set

mongodb.default.replicaset {
host1.host = "localhost"
host1.port = 27017
host2.host = "localhost"
host2.port = 27018
host3.host = "localhost"
host3.port = 27019
}

由于FakeApplication使用默认配置,因此测试失败,因为找不到副本集的主机.我想为测试使用其他配置,基本上删除mongodb.default.replicaset条目.如果mongodb.default.replicaset是一个简单的Map [String,String],这很容易,因为我可以将其添加到additonalConfiguration中,但是当我尝试这样做时,它将失败,因为期望值类型不是String而是Object.我还尝试通过path参数向FakeApplication提供一个单独的test.conf文件.

As the FakeApplication uses the default configuration the tests fail because the hosts of the replicaset cannot be found. I want to have a different configuration for my tests, basically remove the mongodb.default.replicaset entry. If mongodb.default.replicaset were a simple Map[String, String] that would be easy as I could just add it to the additonalConfiguration but when I try to do that it fails because the expected value type is not a String but an Object. I have also tried to provide a separate test.conf file to the FakeApplication via the path parameter.

override def around[T <% Result](t: => T) = {
    running(FakeApplication(path = new java.io.File("conf/test.conf"), additionalConfiguration = inMemoryMongoDatabase(), additionalPlugins = Seq("se.radley.plugin.salat.SalatPlugin"))) {
        t // execute t inside a http session
    }
}

那也不起作用,因为它没有加载任何配置.

That didn't work either as it didn't load any config.

非常感谢您的帮助.谢谢.

I would greatly appreciate any help. Thanks.

克里斯

推荐答案

问题是使用Play的FakeAppication运行集成测试时如何指定test.conf文件.在集成测试中,我无法调用play -Dconfig.file=conf/test.conf.

The problem is how to specify the test.conf file when running an intergration test using Play's FakeAppication. In my integration test I cannot call play -Dconfig.file=conf/test.conf.

我设法做到的是:

object FakeSalatApp extends Around {

 def EmbeddedMongoTestPort: Int = 27028

 def inMemoryMongoDatabase(name: String = "default"): Map[String, String] = {
   val dbname: String = "play-test-" + scala.util.Random.nextInt
   Map(
     ("mongodb." + name + ".db" -> dbname),
     ("mongodb." + name + ".port" -> EmbeddedMongoTestPort.toString),
     ("mongodb." + name + ".replicaset.host1.host" -> "localhost"),
     ("mongodb." + name + ".replicaset.host1.port" -> EmbeddedMongoTestPort.toString),
     ("mongodb." + name + ".replicaset.host2.host" -> "localhost"),
     ("mongodb." + name + ".replicaset.host2.port" -> (EmbeddedMongoTestPort + 1).toString),
     ("mongodb." + name + ".replicaset.host3.host" -> "localhost"),
     ("mongodb." + name + ".replicaset.host3.port" -> (EmbeddedMongoTestPort + 2).toString))
  }

 override def around[T <% Result](t: => T) = {
   running(FakeApplication(additionalConfiguration = inMemoryMongoDatabase(), additionalPlugins = Seq("se.radley.plugin.salat.SalatPlugin"))) {
     t // execute t inside a http session
   }
 }
}

这篇关于使用测试配置播放2.0 FakeApplication设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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