3

我正在实现一个文件存储服务,它获取一个文件并将其保存到具有特殊元数据的 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 应用程序?

4

1 回答 1

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
}
于 2013-09-16T10:01:26.387 回答