Play2 和 ReactiveMongo 测试问题:测试失败后立即连接数据库



我正在实现一个文件存储服务,该服务正在获取一个文件并将其保存到带有特殊元数据的gridFS中。当然,我想确保一切都在集成中工作 - 文件确实存储在数据库中,然后从中检索。

我使用 Play Framework 2.1.3 Scala 和 ReactiveMongo 0.9。

我的测试用例如下所示:

"show empty uploaded size on init" in {
  running(FakeApplication()) {
    Await.result(FileStorage.getFilesSize(profileId), duration) must beNone
  }
}

我试图用running包裹每个案例,或者所有案例,甚至Thread.sleep.但是测试失败数据库总是启动的。

[error] There is no started application
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:51)
[error] play.api.Play$$anonfun$current$1.apply(Play.scala:51)
[error] play.api.Play$.current(Play.scala:51)
[error] content.FileStorage$.db$lzycompute(FileStorage.scala:32)
...
[info] Total for specification FileStorageSpec
[info] Finished in 21 ms
[info] 5 examples, 1 failure, 4 errors
[info] 
[info] application - ReactiveMongoPlugin starting...
[info] application - ReactiveMongoPlugin successfully started with db 'test'! Servers:
        [localhost:27017]
[info] play - Starting application default Akka system.
[info] play - Shutdown application default Akka system.

我做错了什么?您如何测试ReactiveMongo应用程序?

FileStorage对象中,您有以下行:

lazy val db = ReactiveMongoPlugin.db
val gridFS = GridFS(db, "file")
val collection = db.collection[JSONCollection]("file.files")
collection.indexesManager.ensure(Index(Seq("metadata.profileId" -> IndexType.Ascending)))

实例化对象时,将执行上述代码行。由于您无法控制对象实例化,因此无法确定何时发生。

行更改为以下内容可能会有所帮助:

def db = ReactiveMongoPlugin.db
def gridFS = GridFS(db, "file")
def collection = {
  val collection = db.collection[JSONCollection]("file.files")
  collection.indexesManager.ensure(Index(Seq("metadata.profileId" -> IndexType.Ascending)))
  collection
}

最新更新