gpt4 book ai didi

tdd - 使用 expresso - redis session 存储测试 node.js

转载 作者:IT王子 更新时间:2023-10-29 06:03:20 24 4
gpt4 key购买 nike

我正在尝试使用 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>');
});
}

};

最佳答案

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

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

关于tdd - 使用 expresso - redis session 存储测试 node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5452217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com