本文介绍了迁移至Play 2.5后,ReactiveMongo连接未在测试中初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

将我的Pl​​ay(Scala)应用迁移到2.5.3之后,使用ReactiveMongo对我的代码进行的某些测试现在通过了,现在设置失败.

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.

这是我使用ScalaTest的代码:

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)
...
}

在插入行,我得到了:

reactivemongo.core.errors.ConnectionNotInitialized:MongoError ['连接丢失了元数据(例如协议版本等).连接池可能正在初始化.']

我尝试了很多类似使用lazy val而不是def初始化collection的事情.但是没有任何效果.

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.

推荐答案

感谢@cchantep,通过替换上面的代码,测试按预期运行:

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

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

使用此代码

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

换句话说,reactiveMongoApi.database(以及由于Future而进行的适当更改)是必经之路.

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

这篇关于迁移至Play 2.5后,ReactiveMongo连接未在测试中初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 22:59