简体   繁体   中英

Test for Json transformers in Play framework 2.x

I wrote three simple json transformers with validations

val validateDefaultReminderMethod = Reads.pattern("email|sms|popup".r, "error.reminder.method (pattern: email|sms|popup)")

val validateDefaultReminderMinutes = Reads.min(0) keepAnd Reads.max(40320)

val validateDefaultReminder = (
    (__ \ "method").json.pickBranch(Reads.of[JsString] keepAnd validateDefaultReminderMethod) and
      (__ \ "minutes").json.pickBranch(Reads.of[JsNumber] keepAnd validateDefaultReminderMinutes)
    ).reduce

for unit testing first two transformers i can use simple test like

"Default Reminder Method validator" must {
    "is successful for email value" in {
      JsString("email").validate(validateDefaultReminderMethod) must be (JsSuccess("email"))
    }
    ...
}

but I can not understand, how I must test third transformer. It consists of the first two, but how can I check this? So I must wrote same tests for third transformer?

What about:

case class MyClass(method: String, minutes:Int)

"validateDefaultReminder" must {
    "is successful for email value and minutes" in {
        val json = Json.parse("""{ "email: "a@gmail.com", "minutes": 45}""")              
        json.validate[MyClass](validateDefaultReminder) must be JsSuccess
    }
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM