JsResultException error.path.missing错误

时间:2018-06-11 19:41:14

标签: scalatest playframework-2.6

我收到JsResultException(errors:List((/additional-info,List(JsonValidationError(List(error.path.missing),WrappedArray())))错误,虽然json结构对我来说似乎没问题。我犯了什么错误?

我正在编写单元测试。测试用例是如果客户端发送没有json的消息,服务器应该发送错误消息。

发送错误的服务器端代码为Future { Ok(Json.toJson(JsonResultError(messagesApi("error.incorrectBodyType")(langs.availables(0))))) }

测试案例是

   "User signup request with no body" should {
        "return  400 (Bad Request) and the validation text must be 'Incorrect body type. Body type must be JSON'" in {
          println("testing with mocked User value",user);
          val request = FakeRequest("POST","ws/users/signup")
          println("sending request",request)
          val result:Future[Result] = controller.signupUser(request)
          val bodyAsString = contentAsString(result)
          println("body as String: "+bodyAsString)
          val bodyAsJson = Json.toJson(bodyAsString)
          println("body as json:"+bodyAsJson)

          val jsonResultError:JsonResult = bodyAsJson.as[JsonResult]
          println("jsonResultError: "+jsonResultError)
          (status(result) mustBe OK )
          (jsonResultError.message mustBe ("some Error "))

        }
      }
    }

控制台上的打印消息是

body as String: {"result":"error","additional-info":"error.incorrectBodyType"}
body as json:"{\"result\":\"error\",\"additional-info\":\"error.incorrectBodyType\"}"

JsResultException(errors:List((/additional-info,List(JsonValidationError(List(error.path.missing),WrappedArray()))), (/result,List(JsonValidationError(List(error.path.missing),WrappedArray())))))
play.api.libs.json.JsResultException: JsResultException(errors:List((/additional-info,List(JsonValidationError(List(error.path.missing),WrappedArray()))), (/result,List(JsonValidationError(List(error.path.missing),WrappedArray())))))

implicit和我的模型之间的json次转换是

sealed abstract class JsonResult (val result:String, val message:String)
case class JsonResultError(override val message:String) extends JsonResult(result="error", message)
case class JsonResultSuccess(override val message:String) extends JsonResult(result="success",message)

object JsonResult {

  def apply(result:String,additionalInfo:String) = {
    if(result== "error") JsonResultError(additionalInfo) else JsonResultSuccess(additionalInfo)
  }

  def unapply(jsonResult:JsonResult):Option[(String,String)] = {
    if(jsonResult == null) None else Some (jsonResult.result,jsonResult.message)
  }
}


object JSONMessagesImplicits {

  implicit val JsonResultWrites:Writes[JsonResult] = {
    ((JsPath \ "result").write[String] and
      (JsPath \ "additional-info").write[String])(unlift(JsonResult.unapply)) //I suppose `unlift` would convert Option[T] return value of unapply method of JsonResult Extractor object to T.
     }

  //read from jsvalue i.e. create model from jsvalue
  implicit val JsonResultReads:Reads[JsonResult] = {
    ((JsPath \ "result").read[String] and
      (JsPath \ "additional-info").read[String])(JsonResult.apply _)
  }

}

1 个答案:

答案 0 :(得分:0)

当我将val bodyAsJson = Json.toJson(bodyAsString)更改为val bodyAsJson = Json.parse(bodyAsString)

时,它有效