会同时检查正则表达式和null的协定匹配器

时间:2019-01-10 09:47:43

标签: regex pact pact-net

我有下面的pact文件,其中包含以下交互信息

"interactions": [
{
"description": "I call fixture service using a valid fixture Id",
"providerState": "a request to check the api response",
"request": {
"method": "get",
"path": "/api/v1.0/abc/5d550d86-fe18-44e5-93d2-817318acca3d",
"headers": {
"Accept": "application/json",
"X-Clarksons-Security-Cloud": "xxxxyyyyy"
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": {
"fixtureId": "5d550d86-fe18-44e5-93d2-817318acca3d",
"fixtureNumber": "145393-02-DR-03-18"
},
"matchingRules": {
"$.body.fixtureId": {
"match": "type"
},
"$.body.fixtureNumber": {
"match": "regex",
"regex": "[0-9]{6}-[0-9]{2}-[a-zA-Z]{2}-[0-9]{2}-[0-9]{2}"
}
}
}
}
],

fixtureNumber可能为空。请您让我知道一个匹配器,它将同时检查[0-9]{6}-[0-9]{2}-[a-zA-Z]{2}-[0-9]{2}-[0-9]{2}并同时检查是否为空。

2 个答案:

答案 0 :(得分:0)

您可以检查空字符:\x00

该模式类似于:

([0-9]{6}-[0-9]{2}-[a-zA-Z]{2}-[0-9]{2}-[0-9]{2})|\x00

答案 1 :(得分:0)

您不能同时测试是否存在(使用正则表达式)字段。您应该编写两个单独的测试,针对每个用例进行测试,以确保您的代码能够正确处理它。

请阅读这篇文章,其中解释了原因:https://docs.pact.io/faq#why-is-there-no-support-for-specifying-optional-attributes

  

...如果Pact支持断言元素$.body.name可能出现在响应中,那么您编写可以处理可选$ .body.name的使用者代码,但实际上提供者提供了{ {1}},没有任何测试会告诉您您做出了错误的假设。请记住,提供商可能会在不违反合同的情况下返回额外的数据,但它必须至少提供您期望的数据。

     

指定“ SOME_VALUE或null”也是如此。如果您的所有提供者验证测试数据都为此密钥返回空值,则您可能认为您已经验证了“ SOME_VALUE”,但实际上从未验证过。在生产中,您可能会获得与此键完全不同的“ SOME_VALUE”,这可能会导致问题。