2012-08-10 7 views
19

Ich versuche, eine Aktion auf einem Controller zu testen.Play 2 - Scala FakeRequest mitJsonBody

Es ist eine ziemlich einfache Aktion, es JSON nimmt und JSON:

def createGroup = Action(parse.json) { request => 
    val name = (request.body \ "name").as[String] 
    val collabs = (request.body \ "collabs").as[List[String]] 


    Ok(Json.toJson(
     Map("status" -> "OK", 
     "message" -> "%s created".format(name)) 
    )) 
    } 

ich, dass die JSON zurück überprüfen möchten in der Tat richtig ist.

Wie würde ich FakeRequest verwenden, um dies zu tun?

+0

ähnlich: http://stackoverflow.com/questions/28247112/playframework-fakerequest-reuturns-400-error – ses

Antwort

19

Vielleicht so etwas wie:

"POST createGroup with JSON" should { 
    "create a group and return a message" in { 
    implicit val app = FakeApplication() 
    running(app) { 
     val fakeRequest = FakeRequest(Helpers.POST, controllers.routes.ApplicationController.createGroup().url, FakeHeaders(), """ {"name": "New Group", "collabs": ["foo", "asdf"]} """) 

     val result = controllers.ApplicationController.createGroup()(fakeRequest).result.value.get 

     status(result) must equalTo(OK) 
     contentType(result) must beSome(AcceptExtractors.Accepts.Json.mimeType) 

     val message = Region.parseJson(contentAsString(result)) 

     // test the message response 
    } 
    } 
} 

Hinweis: Die val result Linie jetzt korrekt sein könnte, da ich es von einem Test nahm, die einen Async-Controller verwendet.

+1

für mich gearbeitet, wenn innen 'FakeRequest' der' body' gesetzt wurde ein JsValue (es wurde geparst) statt einer einfachen Zeichenfolge –

+1

schlägt auf ".result.value" für mich. Highlights "Ergebnis" – ses

+0

'FakeHeaders()' funktioniert nicht in 2.6, weil die 'Host' Header nicht sein present gibt an, dass es eine Sicherheitswarnung gibt und lehnt die Anfrage ab. Außerdem muss der Content-Type-Header auf text/json gesetzt werden. Also musste ich folgendes tun: 'val postHeaders = FakeHeaders (Liste (" HOST ") -> "localhost", "content-type" -> "text/json")), dann: 'FakeRequest (POST, Pfad, postHeaders, body)' –

4

Ich hatte das gleiche Problem. Problem behoben:

"respond to the register Action" in { 
    val requestNode = Json.toJson(Map("name" -> "Testname")) 
    val request = FakeRequest().copy(body = requestNode) 
     .withHeaders(HeaderNames.CONTENT_TYPE -> "application/json"); 
    val result = controllers.Users.register()(request) 

    status(result) must equalTo(OK) 
    contentType(result) must beSome("application/json") 
    charset(result) must beSome("utf-8") 

    val responseNode = Json.parse(contentAsString(result)) 
    (responseNode \ "success").as[Boolean] must equalTo(true) 
    } 
+0

hat iteratee anstelle von 'Si zurückgegeben mpleResult' – zinking

8

Ich benutze Play 2.1. @EtienneK Methode funktioniert nicht für mich. Dies ist, wie ich benutze:

"update profile with new desc" in { 
      running(FakeApplication()) { 
     var member1 = new MemberInfo("[email protected]") 
     member1.save() 
     var mId = member1.getMemberIdString() 

     val json = Json.obj(
      "description" -> JsString("this is test desc") 
       ) 
     val req = FakeRequest(
        method = "POST", 
        uri = routes.ProfileApiV1.update(mId).url, 
        headers = FakeHeaders(
        Seq("Content-type"->Seq("application/json")) 
       ), 
        body = json 
       ) 
       val Some(result) = route(req.withCookies(Cookie("myMemberId", mId))) 
     status(result) must equalTo(OK) 
     contentType(result) must beSome("application/json") 
     charset(result) must beSome("utf-8") 
     contentAsString(result) must contain("ok") 

     member1 = MemberInfo.getMemberInfoByMemberId(mId) 
     member1.delete() 
     } 
    } 
Verwandte Themen