나는 다음과 같은 작업을플레이 프레임 워크 2 : 본문으로 JSON 문자열로 요청을 테스트
def save() = Action(parse.json) { implicit request =>
request.body.asOpt[IdeaType].map { ideatype =>
ideatype.save.fold(
errors => JsonBadRequest(errors),
ideatype => Ok(toJson(ideatype))
)
}.getOrElse (JsonBadRequest("Invalid type of idea entity"))
}
이 내가 웹 서비스는이 같은 컬과 확인 작업이
을 테스트하고 싶습니다 : 올바르게 새 리소스를 반환
curl -X post "http://localhost:9000/api/types"
--data "{\"name\": \"new name\", \"description\": \"new description\"}"
--header "Content-type: application/json"
{"url":"/api/types/9","id":9,"name":"new name","description":"new description"}
내가 뭔가를 놓친 거지 :
[error] ! add a new ideaType, using route POST /api/types
[error] ClassCastException: java.lang.String cannot be cast to play.api.libs.json.JsValue (IdeaTypes.bak.scala:35)
[error] controllers.IdeaTypes$$anonfun$save$1.apply(IdeaTypes.bak.scala:36)
[error] controllers.IdeaTypes$$anonfun$save$1.apply(IdeaTypes.bak.scala:35)
[error] play.api.mvc.Action$$anon$1.apply(Action.scala:170)
나는이 질문에 대한 조언을 따랐다 :
나는
"add a new ideaType, using route POST /api/types" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val json = """{"name": "new name", "description": "new description"}"""
val Some(result) = routeAndCall(
FakeRequest(
POST,
"/api/types",
FakeHeaders(Map("Content-Type" -> Seq("application/json"))),
json
)
)
status(result) must equalTo(OK)
contentType(result) must beSome("application/json")
val Some(ideaType) = parse(contentAsString(result)).asOpt[IdeaType]
ideaType.name mustEqual "new name"
}
}
로 테스트하려고 해요 그러나 나는 다음과 같은 오류를 받고 있어요 ?
-
김 Stebel 솔루션은 잘 작동하지만, 나는이 같은 withJsonBody으로 시도 :
val jsonString = """{"name": "new name", "description": "new description"}"""
val json: JsValue = parse(jsonString)
val Some(result) = routeAndCall(
FakeRequest(POST, "/api/types").
withJsonBody(json)
)
나는 다음과 같은 오류 얻을 :
[error] ! add a new ideaType, using route POST /api/types
[error] ClassCastException: play.api.mvc.AnyContentAsJson cannot be cast to play.api.libs.json.JsValue (IdeaTypes.bak.scala:35)
[error] controllers.IdeaTypes$$anonfun$save$1.apply(IdeaTypes.bak.scala:36)
[error] controllers.IdeaTypes$$anonfun$save$1.apply(IdeaTypes.bak.scala:35)
어떤 생각을?
에 선
변경? –
"def save() = Action ..."줄에 예외가 발생했습니다 ... – opensas
경로를 붙여 넣으면 호출이 도움이 될 수 있습니다. – wliao