我的验证是验证天气 orgId 是否出现在响应中,并且 orgId 应该包含一些值
我得到的回应是
状态代码 200 和响应正文为空。
现在我有以下实现
Then match $.orgId == '#present'
Then match $.orgId == '#notnull'
在这种情况下,代码通过,理想情况下它应该失败,因为响应主体为空并且响应中不存在 orgId。
我的问题是为什么代码通过
#present
和 #notnull
即使响应体为空
最佳答案
你肯定错过了一些东西。在一个新的场景中尝试这个,看看它是否有效。我们正在硬编码 response
下面,这完全等同于运行时发生的情况,顺便说一句,这是您测试针对不同类型 JSON 的断言的好方法(无需进行任何 HTTP 调用):
* def response = {}
Then match $.orgId == '#present'
Then match $.orgId == '#notnull'
正如您所料,这会导致失败:
assertion failed: path: $.orgId, actual: null, expected: '#present', reason: actual json-path does not exist
因此,如果您仍然卡住,请按照以下流程操作:https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
编辑:如果您的响应是一个空字符串,但您期望的是 JSON,只需执行此操作,它就会无法通过测试,请参阅类型转换:https://github.com/intuit/karate#type-conversion
* json response = response
但是正如文档中提到的,您应该始终尝试匹配“完整的 JSON”,这样就可以了:
* def response = ''
Then match $ contains { orgId: '#notnull' }
编辑:这将在 0.9.4 https://github.com/intuit/karate/issues/814 中修复
关于 Karate 框架 : #notnull and #present are not working in case response is empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56784911/