迁移到 Play 2.5 后,ReactiveMongo ConnectionNotInitialized In Test
ReactiveMongo ConnectionNotInitialized In Test After Migrating to Play 2.5
将我的 Play (Scala) 应用程序迁移到 2.5.3 后,使用 ReactiveMongo 对我的代码进行的一些测试曾经通过,但现在在设置中失败了。
这是我使用 ScalaTest 的代码:
def fixture(testMethod: (...) => Any) {
implicit val injector = new ScaldiApplicationBuilder()
.prependModule(new ReactiveMongoModule)
.prependModule(new TestModule)
.buildInj()
def reactiveMongoApi = inject[ReactiveMongoApi]
def collection: BSONCollection = reactiveMongoApi.db.collection[BSONCollection](testCollection)
lazy val id = BSONObjectID.generate
//Error occurs at next line
Await.result(collection.insert(Person(id = id, slug = "test-slug", firstName = "Mickey", lastName = "Mouse")), 10.seconds)
...
}
在插入行,我得到这个:
reactivemongo.core.errors.ConnectionNotInitialized: MongoError['Connection is missing metadata (like protocol version, etc.) The connection pool is probably being initialized.']
我尝试了很多方法,比如用 lazy val
而不是 def
来初始化 collection
。但是没有任何效果。
任何关于如何使我的测试再次通过的见解都将受到赞赏。
感谢@cchantep,通过替换上面的代码,测试按预期运行:
def collection: BSONCollection = reactiveMongoApi.db.collection[BSONCollection](testCollection)
使用此代码
def collection: BSONCollection = Await.result(reactiveMongoApi.database.map(_.collection[BSONCollection](testCollection)), 10.seconds)
换句话说,reactiveMongoApi.database
(以及由于 Future
而进行的适当更改)是要走的路。
将我的 Play (Scala) 应用程序迁移到 2.5.3 后,使用 ReactiveMongo 对我的代码进行的一些测试曾经通过,但现在在设置中失败了。
这是我使用 ScalaTest 的代码:
def fixture(testMethod: (...) => Any) {
implicit val injector = new ScaldiApplicationBuilder()
.prependModule(new ReactiveMongoModule)
.prependModule(new TestModule)
.buildInj()
def reactiveMongoApi = inject[ReactiveMongoApi]
def collection: BSONCollection = reactiveMongoApi.db.collection[BSONCollection](testCollection)
lazy val id = BSONObjectID.generate
//Error occurs at next line
Await.result(collection.insert(Person(id = id, slug = "test-slug", firstName = "Mickey", lastName = "Mouse")), 10.seconds)
...
}
在插入行,我得到这个:
reactivemongo.core.errors.ConnectionNotInitialized: MongoError['Connection is missing metadata (like protocol version, etc.) The connection pool is probably being initialized.']
我尝试了很多方法,比如用 lazy val
而不是 def
来初始化 collection
。但是没有任何效果。
任何关于如何使我的测试再次通过的见解都将受到赞赏。
感谢@cchantep,通过替换上面的代码,测试按预期运行:
def collection: BSONCollection = reactiveMongoApi.db.collection[BSONCollection](testCollection)
使用此代码
def collection: BSONCollection = Await.result(reactiveMongoApi.database.map(_.collection[BSONCollection](testCollection)), 10.seconds)
换句话说,reactiveMongoApi.database
(以及由于 Future
而进行的适当更改)是要走的路。