我真的在Chai测试方面挣扎,尤其是现在我已经开始使用猫鼬Promises和Async / Await。 对于较长的帖子,我们会提前道歉,但是每个部分对于形成更大的图片都是必不可少的。
这是一个留言板应用程序(熟悉的人都可以使用FCC)。
3种型号的木板,螺纹,带参考号的回复。
创建我的线程架构时,需要一个董事会_id
。
_id
进行硬编码。在数据库中创建了该线程对象 ,但是没有添加board
字段,我也不明白为什么。{}
。因此,我没有什么可以反对的。 newThread
函数确实从data
返回了一个new_thread.save(function (err, data)
对象,因为我对Board / threads子文档数组有了新的_id 线程架构
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 })
})
})
};
CHAI测试
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();
});
});