2015-10-29 4 views
0

Я пытаюсь проверить пыльник с помощью Play 2.4.2, Spec 2,ответ Тест является JsonArray - основа Play 2.4.2 Spec 2 испытания

" test response Returns a json Array" in new WithApplication { 
    val response = route(FakeRequest(GET, "/myservice/xx")).get 

    // ??? test response is a json array 
} 

Что бы способ проверить этот сценарий ?

+0

какая версия игры2 – Barry

+0

play framework 2.4.2 – nish1013

ответ

2

Вот возможность

Контроллер

@Singleton 
    class BarryController extends Controller{ 
    def barry = Action { implicit request => 

    val json: JsValue = Json.parse(""" 
     { 
     "residents" : [ { 
     "name" : "Fiver", 
     "age" : 4, 
     "role" : null 
     }, { 
     "name" : "Bigwig", 
     "age" : 6, 
     "role" : "Owsla" 
     } ] 
     } 
     """) 
    Ok(json) 
    } 
} 

Тест

import org.specs2.mutable._ 
import play.api.mvc._ 
import play.api.test.FakeRequest 
import play.api.test.Helpers._ 
import play.api.test.WithApplication 
import controllers._ 
import play.api.libs.json._ 


class BarryControllerSpec extends Specification { 
    "controllers.BarryController" should { 
     val expectedJson: JsValue = Json.parse(""" 
      { 
      "residents" : [ { 
       "name" : "Fiver", 
       "age" : 4, 
       "role" : null 
      }, { 
       "name" : "Bigwig", 
       "age" : 6, 
       "role" : "Owsla" 
      } ] 
      } 
      """) 
    "respond with JsArray for /barry" in new WithApplication { 
     val result = new controllers.BarryController().barry()(FakeRequest()) 
     status(result) must equalTo(OK) 
     contentType(result) must equalTo(Some("application/json")) 
     //testing class is JsArray. The .get is necessary to get type out of JsLookupResult/JsDefined instance 
     (contentAsJson(result) \ "residents").get must haveClass[JsArray] 
     //testing JSON is equal to expected 
     contentAsJson(result) must equalTo(expectedJson) 
     //test an attribute in JSON 
     val residents = (contentAsJson(result) \ "residents").get 
     (residents(0) \ "age").get must equalTo(JsNumber(4)) 

    } 
    } 
} 

Надеется, что это дает вам некоторые идеи о том, что вы можете проверить или то, что вы можете сделать.

 Смежные вопросы

  • Нет связанных вопросов^_^