空手道:将 jsonPath 发送到另一个功能时,'set' 函数看不到 jsonPath

Karate: When sending jsonPath to another feature, 'set' function don't see that jsonPath

我有两个功能:第一个是我有参数的地方,第二个是我想从第一个功能发送 argumnets 的地方。

第一个特征:

Feature: test
@dev
  Scenario: test

    * def arguments = { value: '123.00', jspath: '..transferData.amount',consent_body: 'classpath:consent_domestic.json' }
    * def createConsentResult = call read('classpath:reuseable/features/changeConsentBody.feature') arguments

第二个特征:

@ignore
Feature: Change consent body - 1 parameter

  Background:
    * url ''
    * call read('classpath:reuseable/features/commonFunction.feature')

  @act
  Scenario : Change consent body - 1 parameter

    * path 'consents'
    * def consentBody = read(consent_body)
    * print "jsonPath: "+jspath
    * set consentBody jspath= value
    When request consentBody
    And method post
    Then status 201

此处 * def consentBody = read(consent_body) 空手道将 'consent_body' 视为变量并使用此变量的值。

此处 * print "jsonPath: "+jspath 空手道将 'jspath' 视为变量并使用此变量的值并打印:[print] jsonPath: ..transferData.amount.

BUT 此处 * set consentBody jspath = value 空手道没有将 'jspath' 视为变量并且不使用此变量的值。相反,空手道显示错误:

changeConsentBody.feature:17 - unexpected path: jspath

在这种情况下,空手道需要将 'jspath' 视为一个变量并使用该变量的值。

抱歉,如果您真的希望人们有耐心去了解问题,那么您的问题必须简化。以后请参考:https://whosebug.com/help/mcve

也就是说,我在这里看到两个明显的问题。顺便说一句,请在=标志的每一侧保持白色-space:

* set consentBody jspath = value

变量不适用于 set 命令。而且你不能使用 JsonPath。如果你有动态路径,你可以这样做,参考:https://github.com/intuit/karate#eval

* def foo = {}
* def path = 'bar'
* eval foo[path] = 'baz'
* match foo == { bar: 'baz' }

如果您想更新 "bulk" 中的数组而不是 ..transferData.amount - 请使用 karate.map() 操作:

* def foo = [{},{}]
* def fun = function(x){ x.transferData = { amount: 100 }; return x }
* def res = karate.map(foo, fun)
* match res == [{ transferData: { amount: 100 } }, { transferData: { amount: 100 } }]

最后我劝你仔细阅读这篇文章,尽量避免 "generic" 测试用例并使用 call,它只会使事情不必要地复杂化: