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

时间:2013-09-13 05:21:08

标签: mongodb scala testing playframework-2.0 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应用程序?

1 个答案:

答案 0 :(得分:2)

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
}