Express 柴试猫鼬的承诺
我真的很难进行Chai测试,尤其是现在我已经开始使用mongoose承诺和Async/Await。 为这篇冗长的文章提前道歉,但每一部分都是形成更大图景所必需的 这是一个留言板应用程序(FCC适用于任何熟悉的人)。 电路板、螺纹和回复的3种型号,参考。 我的线程模式在创建时需要一个BoardExpress 柴试猫鼬的承诺,express,mongoose,chai,Express,Mongoose,Chai,我真的很难进行Chai测试,尤其是现在我已经开始使用mongoose承诺和Async/Await。 为这篇冗长的文章提前道歉,但每一部分都是形成更大图景所必需的 这是一个留言板应用程序(FCC适用于任何熟悉的人)。 电路板、螺纹和回复的3种型号,参考。 我的线程模式在创建时需要一个Board\u id 在我的Chai测试中,我正在硬编码留言板\u id。这个线程对象确实是在数据库中创建的,但是没有添加board字段,我不明白为什么 第二个问题是,CHAI测试在响应中不返回主体-它返回{}。因此,
\u id
\u id
。这个线程对象确实是在数据库中创建的,但是没有添加board
字段,我不明白为什么{}
。因此,我没有什么可以指控的。newThread
函数确实会从new\u thread.save(函数(err,data)
返回一个data
对象,因为我已经将新的id返回到Board/threads子文档数组 const ThreadSchema = new Schema({
"text": {'type': String},
"delete_password": { 'type': String,'select': false},
"created_on": {'type': Date, 'default': new Date()},
"bumped_on": {'type': Date, 'default': new Date()},
"reported": {'type': Boolean,'select': false,'default': false},
"replycount":{'type': Number,'default': 0},
"board": {'type': mongoose.Schema.Types.ObjectId,'ref': 'Board'},
"replies": [{'type': mongoose.Schema.Types.ObjectId,'ref': 'Reply'}]
});
创建一个线程函数
this.newThread = function(req, res) {
let threadText = req.body.text;
let passwordText = req.body.delete_password;
let boardTitle = req.params.board;
const new_thread = new models.Thread({
text: threadText,
delete_password: passwordText,
board:currentBoardId //currentBoardId defined & set earlier
});
new_thread.save(function (err, data) {
models.Board
.findOne({ board_title: boardTitle})
.then(board => {
if (board == null) {
res.json({ message: "Cannot find board named, " + boardTitle });
} else {
board.threads.push(data._id);
board.save(function (err){
if (err) {console.log(err);}
res.end();
});
}
})
.catch(err => {
return res.status(500).json({ message: err.message })
})
})
};
柴氏试验
test('Every field filled in', function(done) {
chai.request(server)
.post('/api/threads/test')
.send({
board: ObjectId('5d8f748a1d788a3be2b9a7b7'), // board test _id Never gets added to database
text: 'POST - new thread - test thread text',
delete_password: 'password'
})
.end(function(err, res){
expect(err).to.be.null;
assert.equal(res.status, 200);
console.log(res.body); // returns {}
done();
});
});
我对第一个问题的大胆猜测是ObjectId('whatver')没有定义,因为您没有指定或引用ObjectId;