使用 mocha/chai 进行测试时比较两个日期
Compare two dates when testing with mocha/chai
我正在使用 "chai": "^4.2.0"
和 "mocha": "^6.1.4",
。
当使用 assert.equal()
比较两个日期时,即使这两个日期看起来相同,我也会得到错误:
这是我的示例测试:
it('check if dates are correctly added', async () => {
let dataArr = [{'rating_date':'6/6/2019','impact_on_price':'Low'}]
let priceServ = new PriceService()
// clear all records
priceServ.clearPriceTable()
// add 1 record
const res = await priceServ.createOrUpdatePrice(dataArr)
// get "all" records from the table that have a certain action attribute
const tableValues = await priceServ.getPricesByField('rating_date')
assert.equal(tableValues[0].rating_date, new Date(dataArr[0].rating_date));
});
对我做错了什么有什么建议吗?
感谢您的回复!
正如我在评论中提到的,assert.equal
检查是否严格相等。尝试比较时间戳:
assert.equal(tableValues[0].rating_date.getTime(), new Date(dataArr[0].rating_date).getTime());
请注意,当日期不同时,错误消息可能会非常难看。有 libraries 个。
Chai 的 assert.deepEqual
正确地比较了 Date
个对象。
const { assert } = require('chai')
const a = new Date(0)
const b = new Date(0)
const c = new Date(1)
assert.deepEqual(a, b) // ok
assert.deepEqual(b, c) // throws
当然,传递给deepEqual
的两个参数必须是Date
对象,而不是string
s或number
s。
有一个名为 chai-datetime 的插件。 https://www.chaijs.com/plugins/chai-datetime/
需要
const chai = require("chai");
const assert = chai.assert;
chai.use(require("chai"));
导入
import chai, { assert } from "chai";
import chaiDateTime from "chai-datetime";
chai.use(chaiDateTime);
// old
assert.equal(tableValues[0].rating_date, new Date(dataArr[0].rating_date));
// with plugin
// compares only the date portion
assert.equalDate(tableValues[0].rating_date, new Date(dataArr[0].rating_date));
// compares timestamps
assert.equalTime(tableValues[0].rating_date, new Date(dataArr[0].rating_date));
// Also!
let date1 = new Date();
let date2 = date1.toISOString();
let date3 = date1.valueOf();
let date4 = date1.getTime();
assert.equalTime(date1, date1);
assert.equalTime(date1, date2);
assert.equalTime(date1, date3);
assert.equalTime(date1, date4);
assert.equalTime(date2, date1);
assert.equalTime(date2, date2);
assert.equalTime(date2, date3);
assert.equalTime(date2, date4);
// and so on.
// There are also other assertions added, such as before-, closeTo-, afterOrEqual-, within-, etc.
let date5 = new Date();
assert.afterOrEqual(date5, date1);
assert.closeTo(date5, date2, 5) // delta = within 5 seconds
注意:我发现在测试时比较时间戳更有用。这样更容易测试。我正在使用 JS 前端连接到 MSSQL 服务器的 Java api。在我的本地版本中,保存生日和培训日期效果很好。当我的主管连接到实时开发服务器时,她每次保存表格时,每个日期都会向后移动一天。那是因为它正在考虑午夜时间和时区混淆或其他原因。因此 JS 会将日期转换为午夜的日期时间,然后将其发送,然后 Java 会在前一天的另一个时区的午夜发回日期时间。我最终将所有设置都设置为中午,因为时区差异不到十二小时。
另一种方法是导入 expect
而不是 assert
。然后,您可以使用 Chai 的深度相等性检查 .eql()
,例如:
expect.eql(tableValues[0].rating_date, new Date(dataArr[0].rating_date));
我更喜欢这种方法,因为失败消息被记录为普通日期,这样更容易修复失败的测试。
我正在使用 "chai": "^4.2.0"
和 "mocha": "^6.1.4",
。
当使用 assert.equal()
比较两个日期时,即使这两个日期看起来相同,我也会得到错误:
这是我的示例测试:
it('check if dates are correctly added', async () => {
let dataArr = [{'rating_date':'6/6/2019','impact_on_price':'Low'}]
let priceServ = new PriceService()
// clear all records
priceServ.clearPriceTable()
// add 1 record
const res = await priceServ.createOrUpdatePrice(dataArr)
// get "all" records from the table that have a certain action attribute
const tableValues = await priceServ.getPricesByField('rating_date')
assert.equal(tableValues[0].rating_date, new Date(dataArr[0].rating_date));
});
对我做错了什么有什么建议吗?
感谢您的回复!
正如我在评论中提到的,assert.equal
检查是否严格相等。尝试比较时间戳:
assert.equal(tableValues[0].rating_date.getTime(), new Date(dataArr[0].rating_date).getTime());
请注意,当日期不同时,错误消息可能会非常难看。有 libraries 个。
Chai 的 assert.deepEqual
正确地比较了 Date
个对象。
const { assert } = require('chai')
const a = new Date(0)
const b = new Date(0)
const c = new Date(1)
assert.deepEqual(a, b) // ok
assert.deepEqual(b, c) // throws
当然,传递给deepEqual
的两个参数必须是Date
对象,而不是string
s或number
s。
有一个名为 chai-datetime 的插件。 https://www.chaijs.com/plugins/chai-datetime/
需要
const chai = require("chai");
const assert = chai.assert;
chai.use(require("chai"));
导入
import chai, { assert } from "chai";
import chaiDateTime from "chai-datetime";
chai.use(chaiDateTime);
// old
assert.equal(tableValues[0].rating_date, new Date(dataArr[0].rating_date));
// with plugin
// compares only the date portion
assert.equalDate(tableValues[0].rating_date, new Date(dataArr[0].rating_date));
// compares timestamps
assert.equalTime(tableValues[0].rating_date, new Date(dataArr[0].rating_date));
// Also!
let date1 = new Date();
let date2 = date1.toISOString();
let date3 = date1.valueOf();
let date4 = date1.getTime();
assert.equalTime(date1, date1);
assert.equalTime(date1, date2);
assert.equalTime(date1, date3);
assert.equalTime(date1, date4);
assert.equalTime(date2, date1);
assert.equalTime(date2, date2);
assert.equalTime(date2, date3);
assert.equalTime(date2, date4);
// and so on.
// There are also other assertions added, such as before-, closeTo-, afterOrEqual-, within-, etc.
let date5 = new Date();
assert.afterOrEqual(date5, date1);
assert.closeTo(date5, date2, 5) // delta = within 5 seconds
注意:我发现在测试时比较时间戳更有用。这样更容易测试。我正在使用 JS 前端连接到 MSSQL 服务器的 Java api。在我的本地版本中,保存生日和培训日期效果很好。当我的主管连接到实时开发服务器时,她每次保存表格时,每个日期都会向后移动一天。那是因为它正在考虑午夜时间和时区混淆或其他原因。因此 JS 会将日期转换为午夜的日期时间,然后将其发送,然后 Java 会在前一天的另一个时区的午夜发回日期时间。我最终将所有设置都设置为中午,因为时区差异不到十二小时。
另一种方法是导入 expect
而不是 assert
。然后,您可以使用 Chai 的深度相等性检查 .eql()
,例如:
expect.eql(tableValues[0].rating_date, new Date(dataArr[0].rating_date));
我更喜欢这种方法,因为失败消息被记录为普通日期,这样更容易修复失败的测试。