我写了三个带验证的简单json变换器
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
对于单元测试前两个变换器,我可以使用简单的测试,如
"Default Reminder Method validator" must {
"is successful for email value" in {
JsString("email").validate(validateDefaultReminderMethod) must be (JsSuccess("email"))
}
...
}
但我无法理解,我必须如何测试第三个变压器。它由前两个组成,但我该如何检查?所以我必须为第三个变压器编写相同的测试?
答案 0 :(得分:0)
怎么样:
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
}
...
}