我有一个Node.js测试,我断言Date类型的两个值应该相等,但AssertionError [ERR_ASSERTION]: Input objects identical but not reference equal测试意外失败。
(简化的)测试代码是:
it('should set the date correctly', () => {
// (Code that gets "myActualDate" from the page under test goes here)
const myExpectedDate = new Date('2020-05-06');
assert.strictEqual(myActualDate, myExpectedDate);
});我应该如何更改此测试代码以使测试通过?
发布于 2020-05-06 23:19:24
测试失败的原因是,根据文档,assert.strictEqual使用SameValue comparison,对于日期(以及大多数其他类型),如果要比较的两个值不是完全相同的对象引用,则会失败。
备选方案1:使用assert.deepStrictEqual而不是strictEqual:
assert.deepStrictEqual(myActualDate, myExpectedDate); // Passes if the two values represent the same date备选方案2:Use .getTime() before comparing
assert.strictEqual(myActualDate.getTime(), myExpectedDate.getTime()); // Passes if the two values represent the same datehttps://stackoverflow.com/questions/61638863
复制相似问题