我如何使用Specs2单元测试这个Play Scala控制器



我有以下控制器代码连接到MongoDB实例,检索一些数据,然后将数据映射到朋友的JSON列表,我如何使用Specs2进行单元测试?

object Friends extends Controller with MongoController {
  def collection: JSONCollection = db.collection[JSONCollection]("friends")
  def list = Action.async {
    val cursor: Cursor[Friend] = collection.find(Json.obj()).cursor[Friend]
    val futureFriendList: Future[List[Friend]] = cursor.collect[List]()
    futureFriendList.map { friends => Ok(Json.toJson(futureFriendList)) }
  }
}

到目前为止你都做了些什么?您可以在测试中使用Await.result从异步调用获取值。

最新更新