我正在使用 "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));
});
有什么建议我做错了吗?
感谢您的回复!
最佳答案
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。
关于javascript - 使用 mocha/chai 测试时比较两个日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57029837/