简体   繁体   English

在Play框架2.x中测试Json变压器

[英]Test for Json transformers in Play framework 2.x

I wrote three simple json transformers with validations 我用验证编写了三个简单的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

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
    }
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Play框架2中使用Json转换器编写可为空的内容 - Write nullable with Json transformers in play framework 2 如何在Play Framework 2.x中实现隐式的Future的Json Writes of Future对象 - How to implement implicit Json Writes of Future object in Play Framework 2.x 如何在Play Framework 2.x中实现嵌入对象的隐式Json Writes - How to implement implicit Json Writes of embedded object in Play Framework 2.x 如何在Play 2.x中强制执行严格的JSON序列化 - How to enforce strict serialization of JSON in Play 2.x 我可以在Play Framework 2.4中从两个没有变压器的模型编写Json吗 - Can I write Json from two model without transformers in Play Framework 2.4 播放json转换器映射可选字段 - Play json transformers map optional field 播放2.x Json将json密钥转换为下划线大小写的camelCase - Play 2.x Json transform json keys to camelCase from underscore case Play Framework 2.x:如何为反向查找(nominatim OSM)制作HTTP-GET? - Play Framework 2.x: How do i make a HTTP-GET for an reverse lookup (nominatim OSM)? Play scala 2.x中的JSON读取组合器不适用于Map [String,_] - JSON Reads combinator in Play scala 2.x doesn't work for Map[String, _] 如何提高从Play框架2.x的JsError.toFlatJson或JsError.toJson返回的错误消息的可读性? - How to improve the error message readability returned from JsError.toFlatJson or JsError.toJson in Play framework 2.x?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM