scalatest - JsResultException error.path.missing 错误
问题描述
尽管结构对我来说似乎没问题,但我遇到了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 _)
}
}
解决方案
当我val bodyAsJson = Json.toJson(bodyAsString)
改为val bodyAsJson = Json.parse(bodyAsString)
推荐阅读
- visual-studio - 如何在 Visual Studio 代码中使用键盘在文件夹之间切换?
- javascript - 如何在 WebView 的 iframe 中注入 CSS?UWP
- python - 仅从 Python 中的 MongoDB 投影中获取内部字段
- javascript - 类名作为函数中的变量,带有附加代码
- angular - Cypress - 以编程方式操作 Angular/NGRX 应用程序
- php - 如何使用 PHP 列出来自 Google 相册的公开相册和照片(从 Picasa 迁移)
- kubernetes - 我的服务没有作为主机名进入 dns,或者我可以用来连接它的任何东西
- c - 有没有办法在一个位集中表示〜27个整数并有效地访问它们?
- docker-compose - Splash + HAProxy + Docker Compose (aquarium) - 正在运行但在浏览器中看不到
- spring-boot - 如何使用 Spring-Boot 配置配置 JOOQ 设置?