-1

I am trying to test some functionality in the app. I have other tests (with scalatest for reactivemongo) and are working, but with this I am getting this exception

[info] - should Persist and find a token * FAILED * [info] The future returned an exception of type: reactivemongo.api.commands.bson.DefaultBSONCommandError, with message: CommandError[code=26, errmsg=ns not found, doc: { [info] ok: BSONDouble(0.0), [info] errmsg: "ns not found", [info] code: BSONInteger(26) [info] }]. (DaosApplicationSpecOneAppPerTest.scala:74)

This is the code for the tests (both are throwing the same error)

class UserTokenDaoMongoSpec extends DaosApplicationSpecOneAppPerTest {
  "UserTokenDao" should {
    "Persist and find a token" in withUserTokenDao { userTokenDao =>
      val future = for {
        _ <- userTokenDao.save(token)
        maybeToken <- userTokenDao.find(token.id)
      } yield {
        maybeToken.map(_ == token)
      }
      whenReady (future) { result =>
        result.get must be (true)
      }
    }

    "Remove a token" in withUserTokenDao { userTokenDao =>
      val future = for {
        _ <- userTokenDao.save(token)
        _ <- userTokenDao.remove(token.id)
        maybeToken <- userTokenDao.find(token.id)
      } yield {
        maybeToken
      }
      whenReady (future) { result =>
        result must be (None)
      }
    }
  }
}

and for brevity, this is the method that inherits

def withUserTokenDao[T](t: UserTokenDao => T):T = running(app) {
  val userTokenDao = new UserTokenDaoMongo
  whenReady (userTokenDao.tokens.drop()) { result =>
    t(userTokenDao)
  }
}

The UserTokenDao implementation

class UserTokenDaoMongo extends UserTokenDao {

  lazy val reactiveMongoApi = current.injector.instanceOf[ReactiveMongoApi]
  val tokens = reactiveMongoApi.db.collection[JSONCollection]("tokens")

  def find(id:UUID):Future[Option[UserToken]] =
      tokens.find(Json.obj("id" -> id)).one[UserToken]

  def save(token:UserToken):Future[UserToken] = for {
    _ <- tokens.insert(token)
  } yield token

  def remove(id:UUID):Future[Unit] = for {
    _ <- tokens.remove(Json.obj("id" -> id))
  } yield ()

}

and this is the model of UserToken

class UserTokenDaoMongo extends UserTokenDao {

  lazy val reactiveMongoApi = current.injector.instanceOf[ReactiveMongoApi]
  val tokens = reactiveMongoApi.db.collection[JSONCollection]("tokens")

  def find(id:UUID):Future[Option[UserToken]] =
      tokens.find(Json.obj("id" -> id)).one[UserToken]

  def save(token:UserToken):Future[UserToken] = for {
    _ <- tokens.insert(token)
  } yield token

  def remove(id:UUID):Future[Unit] = for {
    _ <- tokens.remove(Json.obj("id" -> id))
  } yield ()

}

I am not sure what could be causing the error

Thank you

cchantep
  • 9,118
  • 3
  • 30
  • 41
agusgambina
  • 6,229
  • 14
  • 54
  • 94

1 Answers1

0

It turned out that the problem was that the collection tokens did not exist. I got into the mongo console, then I created the collection and the tests started working.

agusgambina
  • 6,229
  • 14
  • 54
  • 94