我正在尝试使用 mocha 和 supertest 在 express 中测试我的路线。不幸的是,我似乎无法测试我的重定向。
这是相关代码
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 和express 使用的测试套件: https ://github.com/visionmedia/express/blob/master/test/res.redirect.js 我在这里不知所措。谢谢