首页 > 解决方案 > 子功能 __arg 的深层副本可能存在错误

问题描述

将 json 传递给子功能时 * 副本不会执行深层复制,因此两个实例变量都指向相同的数据。

我已经验证 * 当数据没有传递到子功能时,副本确实很深。

父特征


  Feature: calling debug feature

    Scenario:
      * def jsonA = { a: "aValue", children: [{childA: "childAValue" }]}
      * def result = call read('Debug.feature') jsonA

子功能

Feature: debug

  Background:
    * def jsonA = __arg
    * copy jsonACopy = jsonA
    * set jsonACopy $.children[0].childA = 'childAValueUpdated'
    * print "TEST: -------------------", jsonA, jsonACopy

    Scenario:
      * match jsonACopy != jsonA

更新 jsonACopy 后,我不希望 jsonA 被修改。

15:58:10.517 [main] INFO  com.intuit.karate - [print] TEST: ------------------- {
  "a": "aValue",
  "children": [
    {
      "childA": "childAValueUpdated"
    }
  ]
}
 {
  "a": "aValue",
  "children": [
    {
      "childA": "childAValueUpdated"
    }
  ]
}

15:58:10.519 [main] ERROR com.intuit.karate - assertion failed: path: $, actual: {a=aValue, children=[{"childA":"childAValueUpdated"}]}, NOT expected: {a=aValue, children=[{"childA":"childAValueUpdated"}]}, reason: all key-values matched
15:58:10.520 [main] ERROR com.intuit.karate - feature call failed: Debug.feature
arg: {a=aValue, children=[{"childA":"childAValueUpdated"}]}
Debug.feature:10 - path: $, actual: {a=aValue, children=[{"childA":"childAValueUpdated"}]}, NOT expected: {a=aValue, children=[{"childA":"childAValueUpdated"}]}, reason: all key-values matched
CallDebug.feature:6 -
Debug.feature:10 - path: $, actual: {a=aValue, children=[{"childA":"childAValueUpdated"}]}, NOT expected: {a=aValue, children=[{"childA":"childAValueUpdated"}]}, reason: all key-values matched
HTML report: (paste into browser to view) | Karate version: 0.9.2

标签: karate

解决方案


您能否确认这是否与此处打开的问题相同:https ://github.com/intuit/karate/issues/708

您还可以参考 Stack Overflow 上的其他答案:https ://stackoverflow.com/a/55377608/143475

另请参阅上面的未解决问题 - 如果您有任何发现或建议要添加,请发表评论。

现在,请通过进行字符串转换来解决:

* def a = foo
* string b = foo
* json b = b

推荐阅读