我正在尝试在我的播放应用程序中开始为mongodb编写测试。
似乎我无法使其工作,因为在执行保存之前连接已关闭。
以下是结果
[info] UserDaoMongoSpec:
[info] UserDao
[info] - application - ReactiveMongoApi starting...
[info] - application - ReactiveMongoApi successfully started with DB 'test'! Servers:
[localhost:27017]
[info] - should save users and find them by userId
[info] - application - ReactiveMongoApi stopping...
[info] - application - ReactiveMongoApi connections stopped. [Success(Closed)]
[INFO] [12/24/2015 15:36:43.961] [reactivemongo-akka.actor.default-dispatcher-4] [akka://reactivemongo/user/Monitor-3] Message [reactivemongo.core.actors.Close$] from Actor[akka://reactivemongo/deadLetters] to Actor[akka://reactivemongo/user/Monitor-3#1192398481] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
[info] ScalaTest
[info] Run completed in 3 seconds, 989 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
这是测试的代码,它没有测试任何东西,但我首先尝试编写数据库。
package test.daos
import scala.concurrent.ExecutionContext.Implicits.global
import daos.impl.UserDaoMongo
import org.scalatest._
import play.api.test._
import play.api.test.Helpers._
import org.scalatestplus.play._
class UserDaoMongoSpec extends DaosApplicationSpecOneAppPerTest {
"UserDao" should {
"save users and find them by userId" in {
val userDao = new UserDaoMongo
val future = for {
_ <- userDao.save(credentialsTestUser)
maybeUser <- userDao.find(credentialsTestUser.id)
} yield maybeUser.map(_ == credentialsTestUser)
}
}
}
dao实现
package daos.impl
import java.util.UUID
import scala.concurrent.Future
import play.api.Play.current
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.json.Json
import play.modules.reactivemongo.ReactiveMongoApi
import play.modules.reactivemongo.json._
import play.modules.reactivemongo.json.collection.JSONCollection
import com.mohiva.play.silhouette.api.LoginInfo
import models.{User, Profile}
import models.User._
import daos.api.UserDao
import play.api.Logger
class UserDaoMongo extends UserDao {
lazy val reactiveMongoApi = current.injector.instanceOf[ReactiveMongoApi]
val users = reactiveMongoApi.db.collection[JSONCollection]("users")
def find(loginInfo:LoginInfo):Future[Option[User]] =
users.find(Json.obj("profiles.loginInfo" -> loginInfo)).one[User]
def find(userId:UUID):Future[Option[User]] ={
Logger.debug("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
users.find(Json.obj("id" -> userId)).one[User]
}
def save(user:User):Future[User] =
users.insert(user).map(_ => user)
def confirm(loginInfo:LoginInfo):Future[User] = for {
_ <- users.update(Json.obj(
"profiles.loginInfo" -> loginInfo
), Json.obj("$set" -> Json.obj("profiles.$.confirmed" -> true)))
user <- find(loginInfo)
} yield user.get
def link(user:User, profile:Profile) = for {
_ <- users.update(Json.obj(
"id" -> user.id
), Json.obj("$push" -> Json.obj("profiles" -> profile)))
user <- find(user.id)
} yield user.get
def update(profile:Profile) = for {
_ <- users.update(Json.obj(
"profiles.loginInfo" -> profile.loginInfo
), Json.obj("$set" -> Json.obj("profiles.$" -> profile)))
user <- find(profile.loginInfo)
} yield user.get
}
我可能做错了什么?
谢谢
答案 0 :(得分:-1)
如评论中所述,事实证明错误在于我没有等待未来的完成。我决定使用ScalaTest特定函数来进行测试,等待期货完成
这是代码
"UserDao" should {
"save users and find them by userId" in withUserDao { userDao =>
val future = for {
user <- userDao.save(credentialsTestUser)
maybeUser <- userDao.find(credentialsTestUser.id)
} yield {
maybeUser.map(_ == credentialsTestUser)
}
whenReady (future) { result =>
result.get must be (true)
}
}
}
以防万一,我覆盖了默认行为,因为默认时间不够
implicit override val patienceConfig =
PatienceConfig(timeout = Span(2, Seconds), interval = Span(5, Millis))
谢谢