testing node.js with expresso - redis session store
I am trying to learn TDD with node.js. I've hit this issue with expresso where the expresso
command just hangs, I think because of the redis-server. Killing the process with ctrl+C
finally gives the output I'm looking for (100% 3 tests passed).
What's causing the expresso
command to hang, and what can I do about it?
My app looks like this:
// Module dependencies.
var auth = require('connect-auth'),
RedisStore = require('connect-redis');
var express = require('express');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ store: new RedisStore, secret: "secret goes here" }));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', function(req, res){
res.render('index', {
title: 'Orchestrate'
});
});
app.get('/login', function(req, res){
res.render('user/login', {
title: 'Login'
});
});
app.get('/register', function(req, res){
res.render('user/login', {
title: 'Register'
});
});
// Only listen on $ node app.js
if (!module.parent) {
app.listen(3000);
console.log("Express server listening on port %d", app.address().port);
}
And my tests:
// Run $ expresso
/**
* Module dependencies.
*/
var app = require('../app'),
assert = require('assert');
module.exports = {
'GET /': function(){
assert.response(app,
{ url: '/' },
{ status: 200, headers: { 'Content-Type': 'text/html; charset=utf-8' }},
function(res){
assert.includes(res.body, '<title>Orchestrate</title>');
});
},
'GET /login': function(){
assert.response(app,
{ url: '/login' },
{ status: 200, headers: { 'Content-Type': 'text/html; charset=utf-8' }},
function(res){
assert.includes(res.body, '<title>Login</title>');
}开发者_JAVA百科);
},
'GET /register': function(){
assert.response(app,
{ url: '/register' },
{ status: 200, headers: { 'Content-Type': 'text/html; charset=utf-8' }},
function(res){
assert.includes(res.body, '<title>Register</title>');
});
}
};
Try writing your tests like this and the test runner should terminate itself when all the tests have finished:
'GET /': function(done){
assert.response(app,
{ url: '/' },
{ status: 200, headers: { 'Content-Type': 'text/html; charset=utf-8' }},
function(res){
assert.includes(res.body, '<title>Orchestrate</title>');
done();
});
The answer is that Mongoose doesn't close the connections itself nicely, which causes issues when using expresso.
You need to add a "mongoose.disconnect()", e.g. I always add a 'tear down' step at the end:
tearDown: function(done){
mongoose.disconnect();
done();
}
Hope that helps.
The problem is that the asserts are printed after asynchronous calls. Your tests are not hanged because of connect-redis, they just don't know when all your tests passed.
I'm having the same problem with
mongoose.connect();
expresso tests terminate as expected as long as i don't connect to the db, otherwise i need to kill the process to see the results.
For people hitting this problem in the future, looks like the successor to Expresso called Mocha solves this problem. It supports true initialization and tear down functions as well as a Done() callback to fix the async completion issue.
精彩评论