Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Date 如何在空手道BDD中验证响应中收到的日期_Date_Karate - Fatal编程技术网

Date 如何在空手道BDD中验证响应中收到的日期

Date 如何在空手道BDD中验证响应中收到的日期,date,karate,Date,Karate,响应中有两个字段,响应中有两个参数。 { 日期1:“2018年12月18日”, 日期2:“2018年11月23日” } 我想测试id date1是否小于今天的日期 并且date2小于请求参数中的某个其他日期。 我不知道如何在空手道模式验证中执行此操作您需要将字符串日期解析为java日期/long * def toTime = """ function(s) { var SimpleDateFormat = Java.type('java.text.SimpleDate

响应中有两个字段,响应中有两个参数。 { 日期1:“2018年12月18日”, 日期2:“2018年11月23日” }

我想测试id date1是否小于今天的日期 并且date2小于请求参数中的某个其他日期。
我不知道如何在空手道模式验证中执行此操作

您需要将字符串日期解析为java日期/long

* def toTime =
    """
    function(s) {
      var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
      var sdf = new SimpleDateFormat("dd-MM-yyyy");
      return sdf.parse(s).time           
    }
    """ 
* def other = "20-11-2018"
* def today = new java.util.Date().time
* def response = { date1: "18-12-2018", date2: "23-11-2018" }
* assert today > toTime(response.date1)
* assert toTime(other) < toTime(response.date2)
*def toTime=
"""
职能{
var SimpleDateFormat=Java.type('Java.text.SimpleDateFormat');
var sdf=新的简化格式(“dd-MM-yyyy”);
返回sdf.parse.time
}
""" 
*def other=“20-11-2018”
*def today=new java.util.Date().time
*def响应={date1:“18-12-2018”,date2:“23-11-2018”}
*assert today>toTime(response.date1)
*断言toTime(其他)
作为彼得·托马斯答案的附录。下面是我如何将其外部化为可调用的.feature文件:

@ignore
Feature: Reusable function to capture info about the date

  # Example:
  # * def dateResponse = call read('classpath:features/test_getdate.feature')
  # * def todaysDate = dateResponse.today
  # * def lastYearDate = dateResponse.oneYearAgo

  Background: Setup
    * def pattern = 'MM/dd/yyyy'
    * def getDate =
      """
      function() {
        var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
        var sdf = new SimpleDateFormat(pattern);
        var date = new java.util.Date();
        return sdf.format(date);
      } 
      """

  Scenario: Capture todays date
    * def today = getDate()
    * karate.log("Today's date is: " + today )

  Scenario: Capture a year old date
    * def today = getDate()
    * def getSubtractedYear =
      """
      function(s) {
        var DateTimeFormatter = Java.type("java.time.format.DateTimeFormatter");
        var LocalDate = Java.type("java.time.LocalDate");
        var ChronoUnit = Java.type("java.time.temporal.ChronoUnit");
        var dtf = DateTimeFormatter.ofPattern(pattern);
        try {
          var adj = LocalDate.parse(today, dtf).minusMonths(12);
          return dtf.format(adj);
        } catch(e) {
          karate.log('*** date parse error: ', s);
        }
      } 
      """
    * def oneYearAgo = getSubtractedYear()
    * karate.log("One year ago is: " + oneYearAgo)
然后我像这样测试它:

@mysuite
Feature: Get information about the date

  Background: Setup
    * def dateResponse = call read('classpath:features/test_getdate.feature')

  Scenario: Extract todays date
    * def todaysDate = dateResponse.today
    * karate.log("Captured todays date: " + todaysDate)

  Scenario: Extract last years date
    * def lastYearDate = dateResponse.oneYearAgo
    * karate.log("Captured last years date: " + lastYearDate)

您好,感谢您的回复,但我需要将date1与当前日期进行比较,并将date2与一些请求输入日期进行比较,很抱歉在回复中添加date1,该日期具有时间戳,我想忽略该时间戳。谢谢您宝贵的输入。如果这个日期字段是数组的一部分,我还有一个问题:[{date1:“18-12-2018”,date2:“23-11-2018”},{date1:“18-12-2018”,date2:“23-11-2018”},{date1:“18-12-2018”,date2:“23-11-2018”}。我想执行*assert each a[*].date1我尝试了assert toTime(a[0].date1)<今天可以运行,但我想测试数组的每个date1,然后匹配每个响应。Data.a包含{“date1”:“#?toTime()<今天”}但事实并非如此working@KarateCoder如果需要,请打开一个新问题。请不要教年轻人使用早已过时且臭名昭著的
SimpleDateFormat
类。至少不是第一个选择。而且不是毫无保留的。而且它的
DateTimeFormatter
,您已经在另一个方法中使用了,要好得多。对不起,我从Peter Thomas在同一个线程的回答中直接得到了SimpleDateFormat示例。对我来说,这里重要的一点是在文档化的解决方案上显示一个很好的变体,它表明您可以在这里做不同的事情来实现目标。