Я пытаюсь проверить свои маршруты в экспресс, используя mocha и supertest. К сожалению, я не могу проверить свои переадресации.mocha/express/supertest: 'Ошибка: done() называется несколько раз'
Вот соответствующий код
var should = require('should');
var request = require('supertest');
var app = require('../app');
describe('GET /',function() {
it('should redirect to the login screen',function(done) {
request(app)
.get('/')
.end(function(err,res) {
if(err) {
console.log("ERR: "+err);
done(err);
} else {
console.log("RES:\n",res);
res.headers.should.have.property('location','/login');
done();
}
});
});
Мой app.js имеет это:
app.get('/', routes.index);
Мой routes.index выглядит следующим образом:
exports.index = function(req, res){
if(!req.body.username) {
res.redirect('/login');
}
res.render('index', { title: 'Express' });
};
и мой Войти страница:
exports.login = function(req,res) {
res.render('login',{title: 'Please login'});
};
Я получаю следующее сообщение об ошибке, когда я запускаю тест, который заставляет меня думать, что это на самом деле работает более чем один раз:
collection router
GET/
◦ should redirect to the login screen: GET/302 221ms - 40b
1) should redirect to the login screen
2) should redirect to the login screen
8 passing (281 ms)
2 failing
1) collection router GET/should redirect to the login screen:
Error: done() called multiple times
at multiple (development/git/ivorytower/node_modules/mocha/lib/runnable.js:175:31)
at done (/development/git/ivorytower/node_modules/mocha/lib/runnable.js:181:26)
at development/git/ivorytower/node_modules/mocha/lib/runnable.js:197:9
at development/git/ivorytower/test/routes.js:15:4
at Test.assert (development/git/ivorytower/node_modules/supertest/lib/test.js:190:3)
at development/git/ivorytower/node_modules/supertest/lib/test.js:119:10
at Test.Request.callback (development/git/ivorytower/node_modules/supertest/node_modules/superagent/lib/node/index.js:573:30)
at Test.<anonymous> (development/git/ivorytower/node_modules/supertest/node_modules/superagent/lib/node/index.js:133:10)
at Test.EventEmitter.emit (events.js:95:17)
at IncomingMessage.<anonymous> (development/git/ivorytower/node_modules/supertest/node_modules/superagent/lib/node/index.js:703:12)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:910:16
at process._tickCallback (node.js:415:13)
2) collection router GET/should redirect to the login screen:
Error: done() called multiple times
at multiple (development/git/ivorytower/node_modules/mocha/lib/runnable.js:175:31)
at done (development/git/ivorytower/node_modules/mocha/lib/runnable.js:181:26)
at development/git/ivorytower/node_modules/mocha/lib/runnable.js:197:9
at development/git/ivorytower/test/routes.js:15:4
at Test.assert (development/git/ivorytower/node_modules/supertest/lib/test.js:190:3)
at development/git/ivorytower/node_modules/supertest/lib/test.js:119:10
at Test.Request.callback (development/git/ivorytower/node_modules/supertest/node_modules/superagent/lib/node/index.js:573:30)
at Test.<anonymous> (development/git/ivorytower/node_modules/supertest/node_modules/superagent/lib/node/index.js:133:10)
at Test.EventEmitter.emit (events.js:95:17)
at IncomingMessage.<anonymous> (development/git/ivorytower/node_modules/supertest/node_modules/superagent/lib/node/index.js:703:12)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:910:16
at process._tickCallback (node.js:415:13)
npm ERR! weird error 2
npm ERR! not ok code 0
Я посмотрел и нашел некоторые, возможно, связанных с ошибками, но они почти в течение года, и я следую предложенным обходным решениям безрезультатно: https://github.com/visionmedia/supertest/issues/11#issuecomment-20251424 и набор тестов, используемый экспресс: https://github.com/visionmedia/express/blob/master/test/res.redirect.js Я здесь в затруднении. Благодаря
Ну, я понял это ... Я забыл добавить возврат в свой код маршрутизатора, поэтому на самом деле я вызывал перенаправление И рендер в том же объекте ответа. Неудивительно, что произошла ошибка синтаксического анализа. –