我目前在摩卡咖啡测试中遇到问题。我想测试从mongodb数据库返回对象的getById方法。除日期比较外,一切正常。这就是我的工作。
describe('Service.Step.getById',function(){
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"
});
});
现在的问题是,显然猫鼬会返回一个Date对象,而不仅仅是一个字符串。 (这就是测试显示的内容)
- “已更新”:[日期:2018-09-05T15:24:11.779Z]
- “已更新”:“ 2018-09-05T15:24:11.779Z”
但是如果我将断言中的Created(或Updated)替换为
Created : new Date("2018-09-05T15:24:11.779Z")
我的测试完全失败。你知道我该怎么解决吗?
答案 0 :(得分:2)
等于主张实际和预期的非严格相等(==)。 例如
{a:1} == {a:1} //false
deepEqual 断言 actual 等于 expected
assert.deepEqual({ tea: 'green' }, { tea: 'green' }); //true
答案 1 :(得分:0)
好的。答案很简单。看来日期会嵌套对象,并且
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")
});
});