首页 > 解决方案 > 如何验证邮递员范围内的时间戳?

问题描述

我有 setTimeout(function(){}, [8000]); 在预请求脚本中

GET 响应,时间戳在 10 秒内变化

{"deviceId": null,
"deviceName": null,
"timestamp": "2021-04-08T22:28:26Z"
}

我写的测试用例

var current_timestamp = new Date();
current_timestamp.setSeconds(current_timestamp.getSeconds() - 9);

var current_timestamp1 = new Date();
current_timestamp1.setSeconds(current_timestamp1.getSeconds() - 8);


var current_timestamp3 = new Date();
current_timestamp3.setSeconds(current_timestamp3.getSeconds() - 15);

pm.test("Retrieve currentTimeStamp ", function(){    pm.expect(jsonData.items[0].timestamp).to.be.oneOf([current_timestamp.toISOString().split('.')[0] + 'Z',current_timestamp1.toISOString().split('.')[0] + 'Z']);

)];

上面的代码有效,但我想要
10 秒范围内的东西,因为响应时间更长我无法继续在数组中添加项目,所以我尝试了

var jsonData = pm.response.json();
var responseTime = jsonData.items[0].timestamp;
tests["Response time is acceptable"] = _.inRange(responseTime,       current_timestamp1.toISOString().split('.')[0], current_timestamp3.toISOString().split('.')[0]);

这不起作用。有人可以帮忙吗?

标签: timestamprangepostman

解决方案


let moment = require('moment');

pm.test("Check the timestamp range", function () {
    pm.expect(moment( "2021-04-08T22:28:26Z").isBetween("2021-02-08T22:28:26Z",  "2021-04-08T22:28:27Z")).to.be.true;
});

您可以在脚本沙箱中使用moment库。


推荐阅读