我需要创建一个场景大纲,在其中我想要删除 API 中测试 400 错误请求所需的必填字段
示例: JSON 正文:
[
{
"context": {
"x": "{{x}}",
"y": "{{y}}",
"z": "{{z}}"
},
"oId": "",
"oIId": ""
}
]
Scenario Outline: Validate API - 400 Bad Request with <Comments>
Given url baseUrl + path
And
* set VoidItemFromOrderRequest[0].context.x = x
* set VoidItemFromOrderRequest[0].context.y = y
* set VoidItemFromOrderRequest[0].context.z = z
* eval if (orId == "removefield") { karate.remove('APIRequestBody[0].oId') }
* eval if (orId != "removefield") { karate.set('APIRequestBody[0].oId', 1) }
* eval if (orIId == "removefield") { karate.remove('APIRequestBody[0].oIId') }
* eval if (orIId != "removefield") { karate.set('APIRequestBody[0].oIId', 1) }
* request APIRequestBody[0]
When method PUT
Then status 400
* match response.errors.reason == reason
Examples:
|orIId | orId | reason | Comments
| abc | removefield | body.oId is required | Remove oId
| removefield | abc | body.oIId is required | Remove oIId
我在行 eval 中遇到错误,如下所示,我们该如何修复它?
`org.graalvm.polyglot.PolyglotException:TypeError:com.intuit.karate.core.ScenarioBridge 上的 invokeMember(删除)失败,原因是:Arity 错误 - 预期:2 实际:1
场景意图:
仅供参考-
上面代码中已经提到了尝试
请注意,
karate.remove()
有两个参数,第一个是您正在操作的空手道变量名称。
请注意,在最新版本的 Karate 中,您可以直接操作 JS 对象:
* VoidItemFromOrderRequest[0].context.x = x
并且
if
无需评估即可工作:
* if (orId != "removefield") { karate.set('APIRequestBody[0].oId', 1) }
这是一个工作示例,您可以从中获取提示,另请参阅:https://github.com/karatelabs/karate#scenario-outline-enhancements
Feature:
Background:
* def data = { dum: 'a', foo: 'b' }
Scenario Outline:
* if (nofoo) karate.remove('data', 'foo')
* if (addbar) karate.set('data', 'bar', 'c')
* print data
Examples:
| nofoo! | addbar! |
| true | false |
| false | true |
最后,也许你把事情变得过于复杂了,你可以从
Examples
行加载JSON:https://stackoverflow.com/a/73383069/143475