ReactiveMongo ConnectionNotInitialized In Test After Migrating to Play 2.5

倖福魔咒の 提交于 2019-12-24 05:44:23

问题


After migrating my Play (Scala) app to 2.5.3, some tests of my code using ReactiveMongo that once passed now fail in the setup.

Here is my code using 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)
...
}

At the insert line, I get this:

reactivemongo.core.errors.ConnectionNotInitialized: MongoError['Connection is missing metadata (like protocol version, etc.) The connection pool is probably being initialized.']

I have tried a bunch of things like initializing collection with a lazy val instead of def. But nothing has worked.

Any insight into how to get my tests passing again is appreciated.


回答1:


With thanks to @cchantep, the test runs as expected by replacing this code above:

def collection: BSONCollection = reactiveMongoApi.db.collection[BSONCollection](testCollection)

with this code

def collection: BSONCollection = Await.result(reactiveMongoApi.database.map(_.collection[BSONCollection](testCollection)), 10.seconds)

In other words, reactiveMongoApi.database (along with the appropriate changes because of the Future) is the way to go.



来源:https://stackoverflow.com/questions/37191520/reactivemongo-connectionnotinitialized-in-test-after-migrating-to-play-2-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!