删除请求正文中的必填字段作为场景大纲的一部分

问题描述 投票:0回答:1

我需要创建一个场景大纲,在其中我想要删除 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

  • .:程序(未命名:1)`

场景意图:

  1. 使用 context 和 oIId 传递请求正文,但不使用 oId
  2. 使用 context 和 oId 传递请求正文,但不使用 oIId

仅供参考-

  1. 我不是在寻找这些字段的空值,但要求不是在请求正文中发送字段本身
  2. 我可以创建两个单独的场景,但在这里寻找优化,无论是否可行

上面代码中已经提到了尝试

karate
1个回答
0
投票

请注意,

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

© www.soinside.com 2019 - 2024. All rights reserved.