本文介绍了空手道阵列场成为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常奇怪的问题.

I've got this really weird problem.

我想将两个列表添加到一个列表中,但是空手道似乎不支持此功能,因此我编写了一个JS函数.

I want to append two lists into one list, but it seems like Karate does not support this function, so I write a JS function.

function(lists){
    var arr = []
    for each (var list in lists){
        for each (var item in list){
            arr.push(item);
        }
    }
    return arr;
}

然后我编写此功能文件进行测试:

And I write this feature file for testing:

Feature:
  Scenario:
    * def appendList = read('../utils/append-list.js')
    * def arr = [{a: 'a'}, {b: 'b'}, {c: 'c'}]
    * def arr1 = [{a: 'a'}, {b: 'b'}, {c: 'c'}]
    * def arr2 = appendList([arr, arr1])
    * print 'arr2', arr2
    * def xx = { aa: '#(arr2)' }
    * print 'xx', xx
    * copy yy = xx
    * print 'yy', yy
    * match xx == yy

这是日志:

16:00:28.424 [main] INFO  com.intuit.karate - [print] arr2 [
  {
    "a": "a"
  },
  {
    "b": "b"
  },
  {
    "c": "c"
  },
  {
    "a": "a"
  },
  {
    "b": "b"
  },
  {
    "c": "c"
  }
]

16:00:28.444 [main] INFO  com.intuit.karate - [print] xx {
  "aa": {
    "0": {
      "a": "a"
    },
    "1": {
      "b": "b"
    },
    "2": {
      "c": "c"
    },
    "3": {
      "a": "a"
    },
    "4": {
      "b": "b"
    },
    "5": {
      "c": "c"
    }
  }
}

16:00:28.466 [main] INFO  com.intuit.karate - [print] yy {
  "aa": [
    {
      "a": "a"
    },
    {
      "b": "b"
    },
    {
      "c": "c"
    },
    {
      "a": "a"
    },
    {
      "b": "b"
    },
    {
      "c": "c"
    }
  ]
}

16:00:28.469 [main] ERROR com.intuit.karate - assertion failed: path: $.aa, actual: [object Array], expected: [{"a":"a"},{"b":"b"},{"c":"c"},{"a":"a"},{"b":"b"},{"c":"c"}], reason: actual value is not list-like

com.intuit.karate.exception.KarateException: path: $.aa, actual: [object Array], expected: [{"a":"a"},{"b":"b"},{"c":"c"},{"a":"a"},{"b":"b"},{"c":"c"}], reason: actual value is not list-like

    at com.intuit.karate.StepDefs.matchNamed(StepDefs.java:540)
    at com.intuit.karate.StepDefs.matchEquals(StepDefs.java:526)
    at ✽.* match xx == yy(kromotus/test/test.feature:12)


com.intuit.karate.exception.KarateException: path: $.aa, actual: [object Array], expected: [{"a":"a"},{"b":"b"},{"c":"c"},{"a":"a"},{"b":"b"},{"c":"c"}], reason: actual value is not list-like

    at com.intuit.karate.StepDefs.matchNamed(StepDefs.java:540)
    at com.intuit.karate.StepDefs.matchEquals(StepDefs.java:526)
    at ✽.* match xx == yy(kromotus/test/test.feature:12)

我不明白为什么有时数组是数组,有时变成对象?

I don't understand why sometimes the array is the array, sometimes it becomes object?

推荐答案

您可以尝试将JavaScript函数中的返回数据转换为JSON.以下是据我所知解释空手道发生的情况:

You can try to convert the return data in JavaScript function to JSON. Below is explain what happening in karate as my understand:

* def appendList = read('../utils/append-list.js')
* def arr = [{a: 'a'}, {b: 'b'}, {c: 'c'}]
* def arr1 = [{a: 'a'}, {b: 'b'}, {c: 'c'}]
* def arr2 = appendList([arr, arr1]) # return JS[] variable type instead of JSON object
* print 'arr2', arr2
* def xx = { aa: '#(arr2)' } 
* print 'xx', xx
* copy yy = xx
* print 'yy', yy
* match xx == yy

以下是解决方法:

* def appendList = read('append-list.js')
* json arr = [{a: 'a'}, {b: 'b'}, {c: 'c'}]
* json arr1 = [{a: 'a'}, {b: 'b'}, {c: 'c'}]
* json arr2 = appendList([arr, arr1]) # convert to JSON
* print 'arr2', arr2
* json xx = { aa: '#(arr2)' }
* print 'xx', xx
* copy yy = xx
* print 'yy', yy
* match xx == yy

日志信息:

20:37:02.034 [main] WARN com.intuit.karate - skipping bootstrap configuration: could not find or read file: karate-config.js, prefix: CLASSPATH
20:37:02.139 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.142 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.149 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.149 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.168 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.169 [main] INFO com.intuit.karate - [print] arr2 [
  {
    "a": "a"
  },
  {
    "b": "b"
  },
  {
    "c": "c"
  },
  {
    "a": "a"
  },
  {
    "b": "b"
  },
  {
    "c": "c"
  }
]

20:37:02.173 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.177 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.178 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['aa']
20:37:02.179 [main] DEBUG com.jayway.jsonpath.internal.JsonContext - Set path $['aa'] new value [{a=a}, {b=b}, {c=c}, {a=a}, {b=b}, {c=c}]
20:37:02.182 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.183 [main] INFO com.intuit.karate - [print] xx {
  "aa": [
    {
      "a": "a"
    },
    {
      "b": "b"
    },
    {
      "c": "c"
    },
    {
      "a": "a"
    },
    {
      "b": "b"
    },
    {
      "c": "c"
    }
  ]
}

20:37:02.188 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.188 [main] INFO com.intuit.karate - [print] yy {
  "aa": [
    {
      "a": "a"
    },
    {
      "b": "b"
    },
    {
      "c": "c"
    },
    {
      "a": "a"
    },
    {
      "b": "b"
    },
    {
      "c": "c"
    }
  ]
}

20:37:02.189 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
20:37:02.189 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $
1 Scenarios ([32m1 passed[0m)
10 Steps ([32m10 passed[0m)
0m0.680s
html report: (paste into browser to view)

结果

这篇关于空手道阵列场成为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 07:56