gpt4 book ai didi

mongodb - Play2 & ReactiveMongo 测试问题 : db connection right after test fails

转载 作者:可可西里 更新时间:2023-11-01 09:45:17 26 4
gpt4 key购买 nike

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

关于mongodb - Play2 & ReactiveMongo 测试问题 : db connection right after test fails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18778837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com