在空手道中断言 API 提供的排序逻辑的最佳方法是什么?我应该使用 Java 还是 Java 脚本
What is the best way to assert the sorting logic provided by an API in Karate? Should I go with Java or Javascript
注意:- 有一个 API 提供了员工对象列表。我需要确保 empId 字段按升序和降序正确排序。
API 可以接受像 => sortBy=empId:asc 或 sortBy=empId:desc
这样的过滤器参数
[默认排序是升序]
所以 API 看起来像 => baseUrl/employee/v1/employee?sortBy=empId:asc
所以我的代码如下所示
Feature: Verify default sorting
Background:
* def sort =
"""
function (list) {
java.util.Collections.sort(list);
return list;
}
"""
Scenario: Verify default sorting
Given url "api?sortBy=empId:asc"
When method GET
Then status 200
* empId = $response.empList[*].empId
* match empId == sort(empId);
这样做正确吗?
它工作正常,但我不知道它应该用 Java 还是 Java 脚本样式编写。
如有任何建议,我们将不胜感激!!
什么都好,什么都能用。在 1.X 中引入了 karate.sort()
API:https://github.com/intuit/karate#json-transforms
* def foo = [{a: { b: 3 }}, {a: { b: 1 }}, {a: { b: 2 }}]
* def fun = function(x){ return x.a.b }
* def bar = karate.sort(foo, fun)
* match bar == [{a: { b: 1 }}, {a: { b: 2 }}, {a: { b: 3 }}]
如果需要,您可以在 JS 数组上调用 reverse()
作为第二步。
并且使用 Java 代码没有任何问题,只要它有效。
注意:- 有一个 API 提供了员工对象列表。我需要确保 empId 字段按升序和降序正确排序。
API 可以接受像 => sortBy=empId:asc 或 sortBy=empId:desc
这样的过滤器参数[默认排序是升序]
所以 API 看起来像 => baseUrl/employee/v1/employee?sortBy=empId:asc
所以我的代码如下所示
Feature: Verify default sorting
Background:
* def sort =
"""
function (list) {
java.util.Collections.sort(list);
return list;
}
"""
Scenario: Verify default sorting
Given url "api?sortBy=empId:asc"
When method GET
Then status 200
* empId = $response.empList[*].empId
* match empId == sort(empId);
这样做正确吗?
它工作正常,但我不知道它应该用 Java 还是 Java 脚本样式编写。
如有任何建议,我们将不胜感激!!
什么都好,什么都能用。在 1.X 中引入了 karate.sort()
API:https://github.com/intuit/karate#json-transforms
* def foo = [{a: { b: 3 }}, {a: { b: 1 }}, {a: { b: 2 }}]
* def fun = function(x){ return x.a.b }
* def bar = karate.sort(foo, fun)
* match bar == [{a: { b: 1 }}, {a: { b: 2 }}, {a: { b: 3 }}]
如果需要,您可以在 JS 数组上调用 reverse()
作为第二步。
并且使用 Java 代码没有任何问题,只要它有效。