在 Mocha 中比较对象与 MongoDb 的日期
Comparing Objects with Dates from MongoDb in Mocha
我目前在 mocha 中进行测试时遇到问题。我想测试一个从 mongodb 数据库返回对象的 getById 方法。一切正常,除了比较日期。这就是我所做的。
描述('Service.Step.getById',函数(){
before(done => {
mongoose.connect(getConnectionString());
mongoose.connection.on('error', () => {
console.error("Connecting to database failed");
});
mongoose.connection.once('open', () => {
console.log('Connected to Database');
done();
});
});
it('should return a step', async function(){
assert.equal((await StepService.getById("someId")).toObject(), {
Title : "SomeTitle",
_id: mongodb.ObjectID("someId"),
SchemaVersion : "0.0.1",
Description: "SomeDescription",
Video: "Video.mp4",
Image : null,
__v : 0,
Created : "2018-09-05T15:24:11.779Z",
Updated : "2018-09-05T15:24:11.779Z"
});
});
现在的问题是,猫鼬 returns 显然是一个日期对象,而不仅仅是一个字符串。 (这是测试显示的)
- "Updated": [Date: 2018-09-05T15:24:11.779Z]
- "Updated": "2018-09-05T15:24:11.779Z"
但是如果我将断言中的创建(或更新)替换为
Created : new Date("2018-09-05T15:24:11.779Z")
我的测试完全失败了。你知道我该如何解决这个问题吗?
好的。答案其实很简单。似乎 Date 会使对象嵌套并且
assert.equal
将不再适用于此。而是使用
assert.deepEqual
它会按预期工作。正确的代码是
before(done => {
mongoose.connect(getConnectionString());
mongoose.connection.on('error', () => {
console.error("Connecting to database failed");
});
mongoose.connection.once('open', () => {
console.log('Connected to Database');
done();
});
});
it('should return a step', async function(){
assert.deepEqual((await StepService.getById("someId")).toObject(), {
Title : "SomeTitle",
_id: mongodb.ObjectID("someId"),
SchemaVersion : "0.0.1",
Description: "SomeDescription",
Video: "Video.mp4",
Image : null,
__v : 0,
Created : new Date("2018-09-05T15:24:11.779Z"),
Updated : new Date("2018-09-05T15:24:11.779Z")
});
});
equal 断言实际和预期的非严格相等 (==)。
例如
{a:1} == {a:1} //false
deepEqual 断言 actual 深度等于 expected
assert.deepEqual({ tea: 'green' }, { tea: 'green' }); //true
我目前在 mocha 中进行测试时遇到问题。我想测试一个从 mongodb 数据库返回对象的 getById 方法。一切正常,除了比较日期。这就是我所做的。
描述('Service.Step.getById',函数(){
before(done => {
mongoose.connect(getConnectionString());
mongoose.connection.on('error', () => {
console.error("Connecting to database failed");
});
mongoose.connection.once('open', () => {
console.log('Connected to Database');
done();
});
});
it('should return a step', async function(){
assert.equal((await StepService.getById("someId")).toObject(), {
Title : "SomeTitle",
_id: mongodb.ObjectID("someId"),
SchemaVersion : "0.0.1",
Description: "SomeDescription",
Video: "Video.mp4",
Image : null,
__v : 0,
Created : "2018-09-05T15:24:11.779Z",
Updated : "2018-09-05T15:24:11.779Z"
});
});
现在的问题是,猫鼬 returns 显然是一个日期对象,而不仅仅是一个字符串。 (这是测试显示的)
- "Updated": [Date: 2018-09-05T15:24:11.779Z]
- "Updated": "2018-09-05T15:24:11.779Z"
但是如果我将断言中的创建(或更新)替换为
Created : new Date("2018-09-05T15:24:11.779Z")
我的测试完全失败了。你知道我该如何解决这个问题吗?
好的。答案其实很简单。似乎 Date 会使对象嵌套并且
assert.equal
将不再适用于此。而是使用
assert.deepEqual
它会按预期工作。正确的代码是
before(done => {
mongoose.connect(getConnectionString());
mongoose.connection.on('error', () => {
console.error("Connecting to database failed");
});
mongoose.connection.once('open', () => {
console.log('Connected to Database');
done();
});
});
it('should return a step', async function(){
assert.deepEqual((await StepService.getById("someId")).toObject(), {
Title : "SomeTitle",
_id: mongodb.ObjectID("someId"),
SchemaVersion : "0.0.1",
Description: "SomeDescription",
Video: "Video.mp4",
Image : null,
__v : 0,
Created : new Date("2018-09-05T15:24:11.779Z"),
Updated : new Date("2018-09-05T15:24:11.779Z")
});
});
equal 断言实际和预期的非严格相等 (==)。 例如
{a:1} == {a:1} //false
deepEqual 断言 actual 深度等于 expected
assert.deepEqual({ tea: 'green' }, { tea: 'green' }); //true