我可以在空手道中使用具有多种可能类型的模糊匹配器吗?

时间:2018-09-17 16:21:09

标签: karate

我正在测试的API可能为许多字段返回字符串或数字。我已经可以使用自我验证表达式来检查以下行为:{ a: '#? typeof _ === "number" || typeof _ === "string"' }

是否有(或应该有)利用空手道的模糊匹配标记来做到这一点的方法?像{ a: '#string OR #number'}一样?

1 个答案:

答案 0 :(得分:1)

不,我认为这是IMO设计不良的API,我不想夸大语法来解决这些问题。

请注意,您可以按照以下方式使它更加优雅,因此您可以编写一次该函数,“全局”定义它,然后重新使用您的内心内容:

* def isNumOrStr = function(x){ return typeof x === 'number' || typeof x === 'string' }
* def foo = { a: 1 }
* match foo == { a: '#? isNumOrStr(_)' }
* def bar = { a: 'z' }
* match bar == { a: '#? isNumOrStr(_)' }
相关问题