karate - 在空手道中使用逻辑 AND/OR 和模糊匹配标记
问题描述
在空手道中,是否可以使用模糊匹配标记构建逻辑 AND/OR 结构?就像是:
* def response = {a:1, {b:null, c:2}}, {a:2, {b:[x,y,z], c:3}
* match each response == {a:'#number', {b:('#present' && ('#null' || '#array'), c:'#number'}
基本上,检查键b
是否存在 且值为null或JSON数组
解决方案
我建议您将比赛分为两个步骤,以避免过于复杂:
* def response = [{ a: 1, b: null, c: 2 }, { a: 2, b: [x, y, z], c: 3 }]
* match each response contains { b: '#present' }
* match each response == { a: '#number', b: '##array', c: '#number' }
您可以结合“标记”和“自我”参考 - 请参阅此处的最后一个示例:https ://github.com/intuit/karate#self-validation-expressions
* match each response == { a: '#number', b: '##array? _ != null', c: '#number' }
为了完整起见,这里是另一种选择。请注意,该isValid()
函数是可重用的,并且只需要(全局)定义一次。
* def isValid = function(x){ return !x || karate.match(x, '#array').pass }
* match each response == { a: '#number', b: '#? isValid(_)', c: '#number' }
推荐阅读
- c# - 使用 lightinject 拦截在 mvc 5 中使用参数拦截动作控制器
- django - 微服务架构中的 Django admin + 身份验证系统
- java - 在 Java 中使用流将 double[] 转换为 byte[]
- download - Chrome 下载标签不下载文件
- angular - Angular 5.2.9 中用于处理 HTTP 响应的优雅中间件
- haskell - 我如何在 Nats 上定义乘法?
- jquery - jQuery 在尝试验证输入字段时没有响应
- r - 在 r 中合并后的两个数据框显示 <0 rows> (或 0 长度行名称)
- plot - 自定义图例标签 - geopandas.plot()
- encoding - FFmpeg - 将 AC3 5.1 下混到 Fraunhofer FDK ACC 2.1