确保Express App在每个摩卡测试之前运行 [英] Ensuring Express App is running before each Mocha Test

查看:167
本文介绍了确保Express App在每个摩卡测试之前运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ExpressJS,NodeJS,Mongoose和Mocha开发REST API。

I am working on developing a REST API using ExpressJS, NodeJS, Mongoose and Mocha.

事实是我有一个app.coffee文件,它们负责建立ExpressJS并连接到Mongoose。我设置的方式是,首先连接到Mongoose,如果通过,那么ExpressJS应用程序将启动。

The thing is that I have an app.coffee file, thats responsible for setting up ExpressJS and connecting to Mongoose. The way I have set this up is that Mongoose is connected first and if that gets through, then, the ExpressJS App is started.

问题是在设置Mocha ,我需要确保app.coffee中存在的ExpressJS App已经成功启动,包括执行任何测试用例之前的所有异步代码。

The issue is that when setting up Mocha, I need to make sure that ExpressJS App existing in app.coffee is completely started successfully including all asynchronous code before any testcase is executed.

为此,我创建了一个test_helper.coffee并将以下代码放在其中,但是,即使app.coffee中的代码没有完全执行完毕,测试用例也开始执行,这实际上是有意义的:

For that, I have created a test_helper.coffee and placed the following code in it, but, the testcases start their execution even if the code in app.coffee hasn't completed its execution completely which actually makes sense:

before (done) ->
  require(__dirname + '/../src/app')
  done()

简而言之,我想确保在执行任何测试用例之前,ExpressJS应用程序已完全完成其设置。

In a nutshell, I want to make sure that the ExpressJS app has fully completed its setup before any testcase is executed.

我该怎么做? / p>

How I can do that?

推荐答案

我晚了派对,但我发现设置我的摩卡测试套件的最佳方法是快速应用程序是使我的app.js或server.js文件导出应用程序对象,如下所示:

I am late to the party, but I found the best way to set up my mocha test suite for an express app is to make my app.js or server.js file export the app object, like this:

var mongoose = require('mongoose');
var express = require('express');
require('express-mongoose');

var env = process.env.NODE_ENV || 'development';
var config = require('./config/config')[env];

var models = require('./app/models');
var middleware = require('./app/middleware');
var routes = require('./app/routes');

var app = express();

app.set('port', process.env.PORT || config.port || 3000);
app.set('views', __dirname + '/app/views');
app.set('view engine', 'jade');

// database
mongoose.connect(config.db);

// middleware
middleware(app);

// Application routes
routes(app);

app.listen(app.get('port'));
console.log('Express server listening on port ' + app.get('port'));

// export app so we can test it
exports = module.exports = app;

确保您的配置文件具有不同的环境,如开发,测试,生产设置:

make sure your config file has different environments like development, test, production set up:

var path = require('path');
var rootPath = path.normalize(__dirname + '/..');

module.exports = {
  development: {
    db: 'mongodb://localhost/my_dev_db',
    port: 3000
  },
  test: {
    db: 'mongodb://localhost/my_test_db',
    port: 8888
  },
  production: {
    // ...
  }
}

然后在您的测试文件中,您可以继续需要您的应用程序,它将连接到正确的数据库和正确的端口:

then in your test files you can go ahead and require your app, which will connect to the right db and on the right port:

var should = require('chai').should();
var request = require('supertest');
var mongoose = require('mongoose');

var app = require('../app');
var agent = request.agent(app);

var User = mongoose.model('User');

    // get users
    describe('GET /api/users', function() {
      it('returns users as JSON', function(done) {
        agent
        .get('/api/users')
        .expect(200)
        .expect('Content-Type', /json/)
        .end(function(err, res) {
          if (err) return done(err);
          res.body.should.have.property('users').and.be.instanceof(Array);
          done();
        });
      });
    });

最后,要启动整个怪物,您将其包含在您的包中.json (确保你的devDependencies中有nodemon和mocha):

And finally, to start up the whole monster you include this in your package.json (make sure to have nodemon and mocha in your devDependencies):

"scripts": {
    "start": "NODE_ENV=development ./node_modules/.bin/nodemon app.js",
    "test": "NODE_ENV=test ./node_modules/.bin/mocha --reporter spec test/**.js"
  }

现在,您可以使用 npm test 和您的应用程序与 npm开始

Now you can start your test suite with npm test and your app with npm start.

希望它有助于!
ps:从我看到的这个惊人的例子中我学到的大部分东西:
https://github.com/madhums/node-express-mongoose-demo

这篇关于确保Express App在每个摩卡测试之前运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆