2012-08-10 69 views
19

我想在控制器上測試一個動作。玩2 - 斯卡拉FakeRequest withJsonBody

這是一個很簡單的動作,它需要JSON和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)) 
    )) 
    } 

我想驗證JSON返回確實是正確的。

我該如何使用FakeRequest來做到這一點?

+0

相似:http://stackoverflow.com/questions/28247112/playframework-fakerequest-reuturns-400-error – ses 2015-01-31 01:20:43

回答

19

也許是這樣的:

"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 
    } 
    } 
} 

注:val result行現在可能是正確的,因爲我把它從一個使用異步控制器的測試。

+1

爲我工作裏面,當'FakeRequest'了'body'設置成爲一個JsValue(它被解析了)而不是一個簡單的字符串 – 2013-10-02 16:53:48

+1

對我來說「.result.value」失敗。highlight「result」 – ses 2015-01-31 00:30:19

+0

'FakeHeaders()'在2.6中不起作用,因爲'host'現在會導致它提供安全警告並拒絕請求,同時內容類型頭文件需要設置爲text/json,所以我必須做的是:'''''''''''''''''''''''''' - >「localhost」,「content-type」 - >「text/json」))',然後:'FakeRequest(POST,path,postHeaders,body)' – 2018-01-22 07:13:28

4

我有同樣的問題。修正它是這樣的:

"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

返回iteratee而不是'Si mpleResult' – zinking 2014-04-26 10:17:10

8

我使用的是Play 2.1。 @EtienneK方法不適合我。這是我如何使用:

"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() 
     } 
    }