ReactiveMongo ConnectionNotInitialized In Test Aft

2019-07-26 16:49发布

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条回答
你好瞎i
2楼-- · 2019-07-26 17:31

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.

查看更多
登录 后发表回答