问题
i have response like { Data: {a: [{ date1: "2018-12-23", date2: "23-11-2018" },{ date1: "2018-12-20",date2: "23-11-2018" }] } }
and want to test if each date1 field is less than today's date and each date2 is less than input date field (i have a date field in input payload) How can i perform that.
回答1:
In many ways, here is one. Refactoring this code to be cleaner is homework for you :P
* def toTime1 =
"""
function(s) {
var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
var sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.parse(s).time
}
"""
* def toTime2 =
"""
function(s) {
var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
var sdf = new SimpleDateFormat("dd-MM-yyyy");
return sdf.parse(s).time
}
"""
* def input = toTime2("24-11-2018")
* def today = new java.util.Date().time
* def isValid1 = function(x){ return toTime1(x) < today }
* def isValid2 = function(x){ return toTime2(x) < input }
* def response = [{ date1: "2018-12-23", date2: "23-11-2018" }, { date1: "2018-12-20", date2: "23-11-2018" }]
* match each response == { date1: '#? isValid1(_)', date2: '#? isValid2(_)' }
来源:https://stackoverflow.com/questions/54132772/how-to-test-each-date-field-in-the-array-response-using-karate