gpt4 book ai didi

Karate :有没有办法从示例变量中获取 JSON 对象键的长度?

转载 作者:行者123 更新时间:2023-12-04 16:35:51 30 4
gpt4 key购买 nike

我通过了 Karate 考试:

  Scenario Outline: Find all the the prime factors in the range from <start> to <end> are <result>
Given path '/primefactors'
And param start = <start>
And param end = <end>
When method get
Then status 200
And match header content-type contains 'application/json'
And match header content-type contains 'charset=utf-8'
And match response == {numbers:<result>, start:<start>, end:<end>, count:<count>, type:PrimeFactors}
Examples:
| start | end | result | count
| 8 | 10 | {8: [2,2,2], 9:[3,3], 10:[2,5]} | 3
| 13 | 16 | {13: [13], 14:[2,7], 15:[3,5], 16:[2,2,2,2]} | 4

但是,我想做的是没有示例中的计数变量:部分,只是通过这种方式从结果对象变量中的键数的长度导出计数:

 Scenario Outline: Find all the the prime factors in the range from <start> to <end> are <result>
Given path '/primefactors'
And param start = <start>
And param end = <end>
When method get
Then status 200
And match header content-type contains 'application/json'
And match header content-type contains 'charset=utf-8'
And def result = <result>
And match response == {numbers:<result>, start:<start>, end:<end>, count:'#(Object.keys(result).length)', type:PrimeFactors}
Examples:
| start | end | result
| 8 | 10 | {8: [2,2,2], 9:[3,3], 10:[2,5]}
| 13 | 16 | {13: [13], 14:[2,7], 15:[3,5], 16:[2,2,2,2]}

当我尝试这个时,我得到一个错误:

primefactors.feature:33 - javascript evaluation failed: Object.keys(result).length, TypeError: {8=[2,2,2], 9=[3,3], 10=[2,5]} is not an Object in <eval> at line number 1

测试失败。

鉴于 Object.keys(result).length 是有效的 JS(使用 Chrome 开发控制台):

result = {8: [2,2,2], 9:[3,3], 10:[2,5]} 
{8: Array(3), 9: Array(2), 10: Array(2)}
Object.keys(result).length
3

我做错了什么?执行此操作的正确方法是什么?

更新(2019 年 4 月 9 日)以下工作成功:

  Background:
* url baseUrl
* configure lowerCaseResponseHeaders = true
* def keys = function(o){ return o.keySet() }
* def values = function(o){ return o.values() }
* def size = function(o){ return o.size() }

Scenario Outline: Find all the the prime factors in the range from <start> to <end> are <result>
Given path '/primefactors'
And param start = <start>
And param end = <end>
When method get
Then status 200
And match header content-type contains 'application/json'
And match header content-type contains 'charset=utf-8'
And def result = <result>
And match response == {numbers:<result>, start:<start>, end:<end>, count: '#(size(result))', type:PrimeFactors}
Examples:
| start | end | result
| 8 | 10 | {8: [2,2,2], 9:[3,3], 10:[2,5]}
| 13 | 16 | {13: [13], 14:[2,7], 15:[3,5], 16:[2,2,2,2]}

最佳答案

是的, Karate 中的 JS 并不是你在野外看到的真正的 JS,当我们移动到 Graal 时这可能会改变

同时请使用这个技巧从 JSON 中获取键、大小(和值):

Scenario: json behaves like a java map within functions
* def payload = { a: 1, b: 2 }
* def keys = function(o){ return o.keySet() }
* def values = function(o){ return o.values() }
* def size = function(o){ return o.size() }
* json result = keys(payload)
* match result == ['a', 'b']
* json result = values(payload)
* match result == [1, 2]
* def length = size(payload)
* match length == 2

您应该能够在嵌入式表达式中使用函数,例如:'#(keys(foo))'

在未来,我们计划添加一个 karate.keysOf()karate.sizeOf() API 来使这更容易。

关于 Karate :有没有办法从示例变量中获取 JSON 对象键的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55577189/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com