Node.js (完)
我正在使用Mocha和Chai来测试我的Node/Express API,我不明白为什么测试没有到达.end() 以下是测试:Node.js (完),node.js,express,mocha.js,chai,api-design,Node.js,Express,Mocha.js,Chai,Api Design,我正在使用Mocha和Chai来测试我的Node/Express API,我不明白为什么测试没有到达.end() 以下是测试: it('should authenticate successfully with user credentials', function (done) { agent .post('/login') .set('Content-Type', 'application/x-www-form-urlencoded')
it('should authenticate successfully with user credentials', function (done) {
agent
.post('/login')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send({ 'username': 'username', 'password': 'password'})
.end(function (err, res) {
console.log(res);
console.log('***************************Authenticated*********************************************');
expect(res).to.have.status(200);
});
done();
});
这是我要走的路线:
app.post('/login', passport.authenticate('ldapauth', { successRedirect: '/' }));
我想我的问题可能在于没有正式的回应,而是重定向,但我不知道如何处理它 如果您在mocha中测试异步方法,您应该调用回调函数中的调用方法,如下所示
it('should authenticate successfully with user credentials', function (done) {
agent
.post('/login')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send({ 'username': 'username', 'password': 'password'})
.end(function (err, res) {
console.log(res);
console.log('***************************Authenticated*********************************************');
expect(res).to.have.status(200);
done();
});
});
解决方案是将done()回调移到my.end()方法中。谢谢@robertklep我也有同样的问题。在转到另一个函数之前,我想等待.end回调。但我不能用摩卡咖啡,因为我用的是黄瓜。我怎么能等柴,结束回拨? 事实上,我想先记录(1),但它不能正常工作
When('I submit with method {string}:', function (string, docString) {
chai.request(app)
.post(endpoint)
.send(docString)
.end(function (err, res) {
console.log(1)
response = res
})
});
Then('I recieved ok', function () {
console.log(2)
// expect(response.status).to.deep.equal(200)
});
首先将done()
移动到end
处理程序内部。