Supertest不返回结果,节点实例不退出

时间:2014-07-29 22:03:11

标签: node.js express mocha shoulda supertest

我只是在学习测试,我已经在这里学习了几个小时没有成功。每次运行Mocha时,我的测试都显示在终端中并且不返回任何结果,如下所示:

Express test app started on port: 3001

  addition

之后一直空白。此外,节点进程在测试后不会关闭,当它们堆叠起来时,它们会使计算机崩溃。

以下是我的样本测试:

var supertest = require('supertest');
var should = require('should');
process.env.NODE_ENV = 'test';
var app = require('../server');

describe('addition', function() {
    //... previous test
    it('should return 2 given the url /add/1/1', function(done) {
        request(app)
            .get('/add/1/1')
            .expect(200)
            .end(function(err, res) {
                should.not.exist(err);
                parseFloat(res.text).should.equal(2);
                done();
            });
    });
});

这是我的server.js

// Module Depndencies
var express = require('express'),
    fs = require('fs'),
    mongoose = require('mongoose'),
    passport = require('passport'),
    MongoStore = require('connect-mongo')(express),
    flash = require('connect-flash'),
    env = process.env.NODE_ENV = process.env.NODE_ENV || 'development',
    ErrorHandler = require('./app/controllers/api/v1/error_handler_v1'),
    config = require('./config/config'),
    auth = require('./config/middlewares/authorization');


// Connect The Database
mongoose.connect(config.db);
// Catch Database Connection Error
mongoose.connection.on("error", function(err) {
    return console.log("****** Could not connect to mongo server!");
});

// Express Settings
app = express();
app.set('showStackError', true);

//Should be placed before express.static
app.use(express.compress({
    filter: function(req, res) {
        return (/json|text|javascript|css/).test(res.getHeader('Content-Type'));
    },
    level: 9
}));

//Setting the fav icon and static folder - tejkh
app.use(express.favicon());
app.use(express.static(config.root + '/public'));

//Don't use logger for test env
if (process.env.NODE_ENV !== 'test') app.use(express.logger('dev'));

//Set views path, template engine and default layout
app.set('views', config.root + '/app/views');
app.set('view engine', 'jade');

// Enable jsonp
app.enable('jsonp callback');

//Bootstrap models
var models_path = __dirname + '/app/models';
var walk = function(path) {
    fs.readdirSync(path).forEach(function(file) {
        var newPath = path + '/' + file;
        var stat = fs.statSync(newPath);
        if (stat.isFile()) {
            if (/(.*)\.(js|coffee)/.test(file)) {
                require(newPath);
            }
        } else if (stat.isDirectory()) {
            walk(newPath);
        }
    });
};
walk(models_path);

// Bootstrap Passport
require('./config/passport')(passport);


// App.configure
app.configure(function() {

    // CookieParser should be above session
    app.use(express.cookieParser());

    // BodyParser should be above methodOverride
    app.use(express.bodyParser());
    app.use(express.methodOverride());

    // Express/mongo session storage
    app.use(express.session({
        secret: 'secret',
        cookie: {
            maxAge: 864000000 // 10 Days in miliseconds
        },
        store: new MongoStore({
            url: config.db,
            auto_reconnect: true
        })
    }));

    // Connect flash for flash messages
    app.use(flash());

    // Use Passport
    app.use(passport.initialize());
    app.use(passport.session());

    // Route
    app.use(app.router);

    // Error Handler
    app.use(function(err, req, res, next) {
        if (err.uri) delete err.uri;
        if (err.status) {
            return res.status(err.status).jsonp(err);
        } else {
            res.status(500).jsonp({
                name: 'InternalError',
                message: 'Sorry, something went wrong on our end.  We have been notified.',
                code: 'server_error',
                status: 500
            });
        }
    });

}); // app.configure

//Bootstrap routes
require('./config/routes')(app, passport, auth);

// Start App
app.listen(config.port);
console.log('Express ' + env + ' app started on port: ' + config.port);

// Expose app
exports = module.exports = app;

2 个答案:

答案 0 :(得分:2)

试试这个,也许它会起作用

request('http://host:port')

答案 1 :(得分:1)

如果您使用:

var supertest = require('supertest');

然后你应该使用:

supertest(app) 

而不是:

request(app)