最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to compare a string to a date in postman test? - Stack Overflow

programmeradmin1浏览0评论

Suppose a API request fetches a users id, email address and birthday. Sample API Request below:

GET: /v1/users HTTP/1.1
Content-Type: application/json
Authorization: bearer {access_token}

For the above request, the following is the response:

{
    "content": [
        {
            "id": 1,
            "email": "[email protected]",
            "birthday": "1990-01-01"
        },
        {
            "id": 40,
            "email": "[email protected]",
            "birthday": "1990-18-10"
        }
],
    "last": false,
    "total_elements": 2,
    "total_pages": 1,
    "sort": null,
    "first": true,
    "number_of_elements": 2,
    "size": 20,
    "number": 0
}

Now, what will be the test in postman to make sure that all the returned values under birthday node is greater than 1988-18-01?

I have tried the following:

pm.test("Check birthday greater than 1988-18-01", () => {
    for (i = 0; i < jsonData.content.length; i++) {
        var a = '1988-18-01'; 
        pm.expect(jsonData.content[i].birthday).to.be.above(a);
    }
});

But postman says: "Check birthday greater than 1988-18-01 | AssertionError: expected '1990-01-01' to be a number or a date".

Suppose a API request fetches a users id, email address and birthday. Sample API Request below:

GET: /v1/users HTTP/1.1
Content-Type: application/json
Authorization: bearer {access_token}

For the above request, the following is the response:

{
    "content": [
        {
            "id": 1,
            "email": "[email protected]",
            "birthday": "1990-01-01"
        },
        {
            "id": 40,
            "email": "[email protected]",
            "birthday": "1990-18-10"
        }
],
    "last": false,
    "total_elements": 2,
    "total_pages": 1,
    "sort": null,
    "first": true,
    "number_of_elements": 2,
    "size": 20,
    "number": 0
}

Now, what will be the test in postman to make sure that all the returned values under birthday node is greater than 1988-18-01?

I have tried the following:

pm.test("Check birthday greater than 1988-18-01", () => {
    for (i = 0; i < jsonData.content.length; i++) {
        var a = '1988-18-01'; 
        pm.expect(jsonData.content[i].birthday).to.be.above(a);
    }
});

But postman says: "Check birthday greater than 1988-18-01 | AssertionError: expected '1990-01-01' to be a number or a date".

Share Improve this question edited May 9, 2022 at 14:36 bad_coder 12.9k20 gold badges54 silver badges88 bronze badges asked Jan 18, 2018 at 6:03 Noor Yeaser KhanNoor Yeaser Khan 1531 gold badge2 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

So firstly, the dates need to be converted to a format that JS accepts and use the Date constructor to generate the plete date.

Next, the 'above' function in pm accepts an integer, so the date format will not be pared. To fix this, we can convert the date to integer format by using the .getTime() function.

Lastly, it's not a good practice to declare variables inside a for loop. Here's what you can replace your test with:

pm.test("Check birthday greater than 1988-18-01", () => {

   let date, 
   isoFormatDate,
   a = new Date('1988-01-18').getTime();

   for (i = 0; i < jsonData.content.length; i++) {
    
      date = jsonData.content[i].birthday;
            
      isoFormatDate = new Date(date).getTime(); // Converting to integer from date format
    
      pm.expect(isoFormatDate).to.be.above(a);
   }
});
发布评论

评论列表(0)

  1. 暂无评论