我已经开始使用Play和Play-ReactiveMongo插件,并在GET"document by id"场景中测试404响应.不幸的是,而不是Play返回404 NotFound响应,我得到了这个异常:
java.util.NoSuchElementException: JsError.get
at play.api.libs.json.JsError.get(JsResult.scala:11) ~[play_2.10.jar:2.1.1]
at play.api.libs.json.JsError.get(JsResult.scala:10) ~[play_2.10.jar:2.1.1]
at play.modules.reactivemongo.json.collection.JSONGenericHandlers$StructureBufferWriter$.write(jsoncollection.scala:44) ~[play2-reactivemongo_2.10-0.9.jar:0.9]
at play.modules.reactivemongo.json.collection.JSONGenericHandlers$StructureBufferWriter$.write(jsoncollection.scala:42) ~[play2-reactivemongo_2.10-0.9.jar:0.9]
at reactivemongo.api.collections.GenericQueryBuilder$class.reactivemongo$api$collections$GenericQueryBuilder$$write(genericcollection.scala:323) ~[reactivemongo_2.10-0.9.jar:0.9]
at reactivemongo.api.collections.GenericQueryBuilder$class.cursor(genericcollection.scala:333) ~[reactivemongo_2.10-0.9.jar:0.9]
Run Code Online (Sandbox Code Playgroud)
如果id参数与现有文档匹配,则下面的getById函数成功返回单个文档,如果找不到文档,则在"one [JsValue]"行上成功返回异常.
路线文件:
GET /items/:id controllers.ItemsController.getById(id: String)
Run Code Online (Sandbox Code Playgroud)
控制器对象:
object ItemsController extends Controller with MongoController {
def itemsCollection: JSONCollection = db.collection[JSONCollection]("items")
def getById(id: String) = Action {
Async {
val query = Json.obj("_id" -> Json.obj("$oid" ->id))
val futureItem = itemsCollection.
find(query).
one[JsValue]
futureItem.map {
case Some(item) => Ok(item)
case None => NotFound(Json.obj("message" -> …Run Code Online (Sandbox Code Playgroud) 我看到有对Option类型的支持,但是自定义案例类呢?
我有点想这样做:
result match {
case SuccessCase(values) => {
values.foo should be ("bar")
}
case FailureCase => // should fail test, but how to say this in ScalaTest?
}
Run Code Online (Sandbox Code Playgroud)