Mocha/Sinon 在 express 中测试 mongoose
Mocha/Sinon test mongoose inside express
我尝试用 mocha、supertest 和 sinon 测试这条快速路线。测试无法通过承诺,它在 User.find 回调函数中的第一个 mongoose 调用后停止,并带有未决错误消息:
错误:超时超过 2000 毫秒。确保在此测试中调用了 done() 回调。
我在回调中调用了 done() 但没有...
module.exports = function(app, User) {
app.route('/appointments')
.post(function(req,res){
User.find({'department': req.body.department}, function(err, workers){
return workers._id;
}).then(function(allWorkers){
var deferred = Q.defer();
function sortWorkers(worker){
return Appointments.find({worker: worker._id, start: req.body.date});
};
Q.all(_.map(allWorkers, sortWorkers)).done(function (val) {
deferred.resolve(val);
});
return deferred.promise;
}).then(function(workers){
console.log(workers);
})
.catch(function(error) {
console.log(error);
})
.done();
})
};
这是我的开始测试:
it("should save a user and not save the same", function(done){
var appointments = new Appointments({title: 'okok',worker: '580359c86f7159e767db16a9',start:'2015-04-08T02:50:04.252Z' ,department: 95});
console.log('appointments',appointments);
request(app)
.post("/appointments")
.send(appointments)
.expect(200)
.end(function(err,res){
console.log('ok',res);
done();
});
});
首先,您不需要在 User.find
promise
调用 done
此外,您的 app.route('/appointments').post
从未 returns 任何回应,请尝试添加
res.end();
你的承诺 .then
和 .catch
上有 console.log
。您也可以使用 HTTP 状态代码,例如
...
}).then(function(workers){
res.status(200).end();
})
.catch(function(error) {
res.status(500).end();
})
这将确保在您的测试中调用 .end(function(err,res){ ... })
并调用正确的 done
函数。
你的单元测试应该总是return承诺。
您只需要在 request(app):
之前添加 return
return request(app)
.post("/appointments")
. . .
.then(() => {
expect(<your actual value>).to.equal(<expected value>)
})
我找到了解决方案:
在我的一些 .then 函数中没有条件,并且 return 如果 workers 数组例如为空则什么都没有,这就是为什么我的测试 return 超过 2000 毫秒的超时 .
我补充:
User.find({'department': req.body.department}, function(err, workers){
if(workers.length == 0){
res.status(500).json({ message: "Nobody in this department" });
}
return workers;
})...
我尝试用 mocha、supertest 和 sinon 测试这条快速路线。测试无法通过承诺,它在 User.find 回调函数中的第一个 mongoose 调用后停止,并带有未决错误消息:
错误:超时超过 2000 毫秒。确保在此测试中调用了 done() 回调。
我在回调中调用了 done() 但没有...
module.exports = function(app, User) {
app.route('/appointments')
.post(function(req,res){
User.find({'department': req.body.department}, function(err, workers){
return workers._id;
}).then(function(allWorkers){
var deferred = Q.defer();
function sortWorkers(worker){
return Appointments.find({worker: worker._id, start: req.body.date});
};
Q.all(_.map(allWorkers, sortWorkers)).done(function (val) {
deferred.resolve(val);
});
return deferred.promise;
}).then(function(workers){
console.log(workers);
})
.catch(function(error) {
console.log(error);
})
.done();
})
};
这是我的开始测试:
it("should save a user and not save the same", function(done){
var appointments = new Appointments({title: 'okok',worker: '580359c86f7159e767db16a9',start:'2015-04-08T02:50:04.252Z' ,department: 95});
console.log('appointments',appointments);
request(app)
.post("/appointments")
.send(appointments)
.expect(200)
.end(function(err,res){
console.log('ok',res);
done();
});
});
首先,您不需要在 User.find
promise
done
此外,您的 app.route('/appointments').post
从未 returns 任何回应,请尝试添加
res.end();
你的承诺 .then
和 .catch
上有 console.log
。您也可以使用 HTTP 状态代码,例如
...
}).then(function(workers){
res.status(200).end();
})
.catch(function(error) {
res.status(500).end();
})
这将确保在您的测试中调用 .end(function(err,res){ ... })
并调用正确的 done
函数。
你的单元测试应该总是return承诺。 您只需要在 request(app):
之前添加 returnreturn request(app)
.post("/appointments")
. . .
.then(() => {
expect(<your actual value>).to.equal(<expected value>)
})
我找到了解决方案: 在我的一些 .then 函数中没有条件,并且 return 如果 workers 数组例如为空则什么都没有,这就是为什么我的测试 return 超过 2000 毫秒的超时 .
我补充:
User.find({'department': req.body.department}, function(err, workers){
if(workers.length == 0){
res.status(500).json({ message: "Nobody in this department" });
}
return workers;
})...