我正在尝试使用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 我在这里不知所措。 感谢
答案 0 :(得分:0)
问题是我的路由器代码:
exports.index = function(req, res){
if(!req.body.username) {
res.redirect('/login');
}
res.render('index', { title: 'Express' });
};
应该是
exports.index = function(req, res){
if(!req.body.username) {
res.redirect('/login');
} else {
res.render('index', { title: 'Express' });
}
};
所以响应是写入一个重定向,然后立即有一个模板呈现给它(如果我正确理解它)。 if / then / else块(或if块中的返回)修复了问题。