使用expresso-redis会话存储测试node.js

时间:2011-03-27 20:32:50

标签: tdd node.js redis

我正在尝试使用node.js学习TDD。我用expresso来解决这个问题,其中expresso命令只是挂起,我想是因为redis-server。使用ctrl+C终止进程最终会提供我正在寻找的输出(通过100%3测试)。

导致expresso命令挂起的原因是什么,我该怎么办?

我的应用看起来像这样:

// 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);
}

我的测试:

// 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>');
      });
  },
  '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>');
      });
  }

};

5 个答案:

答案 0 :(得分:4)

尝试编写这样的测试,测试运行器应在所有测试完成后自行终止:

  '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();
      });

答案 1 :(得分:3)

答案是,Mongoose并没有很好地关闭连接,这在使用expresso时会引起问题。

您需要添加“mongoose.disconnect()”,例如我总是在最后添加一个“拆卸”步骤:

    tearDown: function(done){
       mongoose.disconnect();
       done();
    }

希望有所帮助。

答案 2 :(得分:1)

问题是在异步调用之后打印断言。由于connect-redis,你的测试没有被挂起,他们只是不知道你的所有测试都通过了。

答案 3 :(得分:1)

我遇到了与

相同的问题
mongoose.connect();

expresso测试按预期终止,只要我没有连接到db,否则我需要终止进程才能看到结果。

答案 4 :(得分:0)

对于将来遇到这个问题的人来说,看起来像名为Mocha的Expresso的继任者解决了这个问题。它支持真正的初始化和拆除功能以及Done()回调以修复异步完成问题。