柴没有到达 .end()
Chai not reaching .end()
我正在使用 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')
.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: '/' }));
我想我的问题可能是没有正式回复,而是重定向,但我不确定如何处理。
如果您正在测试异步方法 int mocha,您应该在回调函数中调用 call 方法,如下所示。
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() 回调移动到我的 .end() 方法中。谢谢@robertklep
我对 chai 请求有同样的问题。在转到另一个函数之前,我想等待 .end 回调。但我不能用摩卡,因为我用的是黄瓜。我如何等待 chai .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)
});
我正在使用 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')
.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: '/' }));
我想我的问题可能是没有正式回复,而是重定向,但我不确定如何处理。
如果您正在测试异步方法 int mocha,您应该在回调函数中调用 call 方法,如下所示。
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() 回调移动到我的 .end() 方法中。谢谢@robertklep
我对 chai 请求有同样的问题。在转到另一个函数之前,我想等待 .end 回调。但我不能用摩卡,因为我用的是黄瓜。我如何等待 chai .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)
});