mardi 30 août 2016

How to Unit test a controller in Play/Scala

I'm having trouble when trying to test a controller that sends a POST request to the API. I've tried different ways of testing it but I keep on getting the same "400" response. I believe the format of the json val is correct because I've tested it using Swagger and it works fine. Can anybody help me to understand what I'm missing here. Many thanks.

val json: JsValue = Json.parse("""[{"id":"1","address":"my address"}]""")
val mockAddressFinder = mock(classOf[AddressFinder])

"Example test" should {
  "should be valid" in {
   val controller = new Match(mockAddressFinder)
   val results = controller.match.apply(FakeRequest(
    POST,
      "test/test",
      FakeHeaders(Seq("Content-type"->("application/json"))),
    json
   ))
   results.onComplete {
    case Success(_) => results.map(s => println("hello" + s.header.status))
    case _=> println("did not work")
  }
}

Aucun commentaire:

Enregistrer un commentaire