用mocha设置/拆卸express.js应用程序
我试图创build一个独立的testing套件使用摩卡,在完美的世界将启动我的express.js应用程序,使用僵尸来呈现一个页面,检查一堆东西,然后拆卸/杀死express.js应用程序。
有没有一个简单/最好的方法来做到这一点?
NB。 我可以在运行testing之前运行快速应用程序服务器,但是如果你不打算修复它们,牦牛有什么好处。
首先,您需要将您的实际应用程序设置为一个模块,并将其导入到实际启动您的应用程序的文件中。 现在,这是独立的,你可以在实际收听之前让应用处于完整的状态。
您应该将应用程序的实际设置移动到单独的文件中,我们将其称为app.js,可以从您运行节点的文件中调用listen,让我们将其称为index.js。
所以,app.js看起来像:
var express = require('express') , routes = require('./routes'); 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(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('/', routes.index);
和index.js看起来像:
var app = require('./app'); app.listen(3000, function(){ console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env); });
这将实际上让你的应用程序加载,让你的应用程序加载到你的unit testing。
在你的unit testing中,你可以通过设置方法和拆卸方法来调用服务器。
在文件test / app_tests.js中:
describe('app', function(){ var app = require('../app'); beforeEach(function(){ app.listen(3000); }); // tests here afterEach(function(){ app.close(); }) });
除了O D的答案。
在express-app.js或其他文件中描述您的应用程序:
module.exports = function (o) { o = o || {}; var app = express(); // app.configure // configure routes if (o.someOption) { // some additional test code } return app; }
在test / 01-some.js中描述testing:
var expressApp = require('../express-app'); describe('some', function () { // just describe needed vars var app, server, port; // describe setup before(function (next) { // create app when app = expressApp({routes: /api\/some\/.*/ /* put here some test options ...*/}); // creating listener with random port server = app.listen(function () { // store port when it ready port = server.address().port; // and go to tests next(); }); }); // tests it('should return valid result', function (done) { // do a simple request to /api/some http.request({ host: 'localhost', port: port, path: '/api/some' }, function (res) { if (res.err) throw new Error(res.err); done(); }); }); // teardown after(function () { // stop listening that port server.close(); }); });
完成。 😉
现在你可以创build任何数量的testing。 build议您在testing中只启用需要的URL和服务,并通过将parameter passing给express-app.js模块来定义它。
更新:
不知道它是如何工作在mocha
但更好地移动before
和after
调用init.js
并加载它与mocha --require init.js
文件应该看起来像这样:
// use it in your mocha tests global.setupEnv = function setupEnv (o, before, after) { // just describe needed vars var env = Object.create(null); // setup before(function (next) { // create app env.app = expressApp(o); // creating listener with random port env.server = env.app.listen(function () { // store port when it ready port = env.server.address().port; env.app.log('Listening on ', env.port); // and go to tests next(); }); }); // teardown after(function () { // stop listening that port env.server.close(); }); return env; }
而在你的testing中:
// requiring dependencies var request = require('request'); describe('api', function () { // describe setup var env = global.setupEnv({ routes: 'api/some' }, before, after); // tests it('should pass', function (done) { request('http://localhost:' + env.port, function (error, response, body) { done(); }); }); });
- 适用于Express + PostgreSQL的最佳会话存储中间件
- 在Jade中快速显示mongodb文档
- Amazon Elastic负载均衡器不会填充x-forward-proto头
- Node.js Express JS – 找不到模块
- Express.js服务器error handling(404)。 在我被允许处理错误之前,服务器就会发出exception并抛出exception
- 快递/护照 – passport.authenticate不是一个function
- 如何将数据块发送到AWS
- 我怎样才能将一个JSON数组从NodeJSstream到postgres
- 与Express.js沟通骨干的最佳方式