Created
December 15, 2012 20:17
-
-
Save mikegehard/4298851 to your computer and use it in GitHub Desktop.
Problems with JSON API tests in Play2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object BetaInvitationRequestController extends Controller { | |
val successResponse = Map("success" -> true) | |
val betaInvitationRequestMapping = Forms.mapping( | |
"email" -> Forms.email | |
)(BetaInvitationRequest.apply)(BetaInvitationRequest.unapply) | |
val betaInvitationRequestForm = Form(betaInvitationRequestMapping) | |
def create = Action { | |
implicit request => { | |
betaInvitationRequestForm.bindFromRequest.fold( | |
formWithErrors => BadRequest(formWithErrors.errorsAsJson), | |
value => Ok(Json.toJson(successResponse) | |
)) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
running(FakeApplication()) { | |
// val json = Json.parse("""{"email":"mike@example.com"}""") | |
// val json = "{\"email\":\"mike@example.com\"}" | |
val json = """{"email":"mike@example.com"}""" | |
// val request = FakeRequest(POST, "/betaInvitationRequest", FakeHeaders(), json) | |
val request = FakeRequest(POST, "/betaInvitationRequest", FakeHeaders(), json).withHeaders(CONTENT_TYPE -> "application/json") | |
val createInvite = route(request).get | |
"returns a 200 response" in { | |
status(createInvite) must equalTo(OK) | |
} | |
"returns a success response in JSON format" in { | |
contentType(createInvite) must beSome.which(_ == "application/json") | |
contentAsString(createInvite) must beEqualTo("{\"success\":true}") | |
} | |
"creates a betaInvite record" in { | |
pending | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I run the above code via curl, it works as expected. When I run the tests I get the following results:
when the submitted email is valid
'400' is not equal to '200'
Expected :200
Actual :400
java.lang.Exception: '400' is not equal to '200'
at test.api.BetaInvitationRequestSpec$$anonfun$1$$anonfun$apply$mcV$sp$1$$anonfun$apply$5$$anonfun$apply$6.apply(BetaInvitationRequestSpec.scala:26)
at test.api.BetaInvitationRequestSpec$$anonfun$1$$anonfun$apply$mcV$sp$1$$anonfun$apply$5$$anonfun$apply$6.apply(BetaInvitationRequestSpec.scala:26)
returns a 200 response
'{"email":["error.required"]}' is not equal to '{"success":true}'
Expected :{"success":true}
Actual :{"email":["error.required"]}
java.lang.Exception: '{"email":["error.required"]}' is not equal to '{"success":true}'
at test.api.BetaInvitationRequestSpec$$anonfun$1$$anonfun$apply$mcV$sp$1$$anonfun$apply$5$$anonfun$apply$8.apply(BetaInvitationRequestSpec.scala:31)
at test.api.BetaInvitationRequestSpec$$anonfun$1$$anonfun$apply$mcV$sp$1$$anonfun$apply$5$$anonfun$apply$8.apply(BetaInvitationRequestSpec.scala:29)
It looks to me like the JSON isn't getting submitted properly.