gpt4 book ai didi

node.js - Mocha MongoDB Mongoose ObjectId 引用在第一个 'it' 语句后消失

转载 作者:太空宇宙 更新时间:2023-11-03 22:36:04 24 4
gpt4 key购买 nike

我正在为 Mongoose 模型编写一些测试,它包含一个引用另一个模型的字段。我运行 before() 并创建一个对象,然后运行测试。在创建的回调中,我看到该对象在正确的字段中有一个 ID。在第一个“it”语句中,我还看到了 ID 和我的测试通过。在接下来的每个“it”语句中,它都会消失并且为空。我发现如果我实际上事先创建了引用的对象,那么该字段仍然存在。我不认为 mongoose/mongo 实际上会检查所有 ObjectId 引用,但如果确实如此,有人知道它是如何/为什么工作的吗?如果不是,到底是什么原因导致了这种现象呢?

消失的字段是 OfficeHoursSchema 中的“主机”字段。顺便说一句,即使将 required 设置为 false,这仍然不起作用。

模型定义:

var appointmentSchema = new Schema({
attendee: { type: Schema.Types.ObjectId, ref: "Student", required: false },
startTime: { type: Date },
duration: Number
});

var statusStates = ['open','closed']

var OfficeHoursSchema = new Schema({
host: { type: Schema.Types.ObjectId, ref: "Employee" , required: true},
appointments: [appointmentSchema],
description: String,
location: String,
startDateTime: { type: Date, default: Date.now },
endDateTime: { type: Date, default: Date.now },
status: { type: String, default: 'open' , enum: statusStates},
seqBooking: {type: Boolean, default: true}
});

测试:

describe('OfficeHours Model', function(){
var hostId = new mongoose.Types.ObjectId;
var mins = 15;
var numAppointments = ohDurationInMs/appointmentDuration;
var officeHour;
var officeHourToCreate = {
host: hostId,
appointments: appointmentsToCreate(),
description: 'meeting',
location: 'room 1',
startDateTime: new Date(startTime), //3/6/2015 at 3:30pm EST
endDateTime: new Date(endTime), //2 hours later. 3/6/2015 at 5:30pm EST
totalDuration: ohDurationInMs/(60*1000)
};

before(function(done){
OfficeHour.create(officeHourToCreate,function(err,createdOH){
officeHour = createdOH;;
done();
});
});

it('1st It statement',function(){
expect(officeHour.host).to.be.ok;
});

it('2nd It statement',function(){
expect(officeHour.host).to.be.ok;
});

});

第一个 it 语句通过,但第二个 .host 字段为 Null。

这基本上是有效的

工作:

before(function(done){
var employee = new Employee({password: 'asdfasdfsafasdf'});
employee.save(function(err,createdEmployee){
officeHourToCreate.host = createdEmployee._id;
OfficeHour.create(officeHourToCreate,function(err,createdOH){
officeHour = createdOH;;
done();
});
})
});

我觉得必须对 ObjectId 是否存在于其他地方进行某种检查,但有人可以向我指出这种行为的一些文档吗?非常感谢您阅读本文。

最佳答案

我认为这是因为第二次调用 before() 时,您引用了实例化的 ObjectId,第二次没有创建一个 new 对象正如你所期望的。

你能试试这个吗:

before(function(done){
OfficeHour.create({
host: new mongoose.Types.ObjectId,
// btw I've never seen that syntax for making a new random ID, but if it does what you want it to do then go ahead!
appointments: appointmentsToCreate(),
description: 'meeting',
location: 'room 1',
startDateTime: new Date(startTime), //3/6/2015 at 3:30pm EST
endDateTime: new Date(endTime), //2 hours later. 3/6/2015 at 5:30pm EST
totalDuration: ohDurationInMs/(60*1000)
}, function(err,createdOH){
officeHour = createdOH;;
done();
});
});

关于node.js - Mocha MongoDB Mongoose ObjectId 引用在第一个 'it' 语句后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28947654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com