1

Mongoose モデル高速アプリ (ルート) のテストの作成に問題があります

私は非常に単純なapp.jsファイルを持っています:

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

// Bootstrap db connection
mongoose.connect(config.db)

// Bootstrap models
var models_path = __dirname + '/app/model';
fs.readdirSync(models_path).forEach(function(file) {
    if (~file.indexOf('.js')) {
        require(models_path + '/' + file);
    }
});

var app = express();

// express settings
require('./config/express')(app, config);

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

if (!module.parent) {
    app.listen(app.get('port'), function() {
        console.log('Server started on port ' + app.get('port'));
    })
}

module.exports = app;

model私のマングースモデルを含むフォルダーがあります。

私はtestフォルダを持っていますaccountTest.js- 次のように見えます:
(これは私のアカウントモデルをテストするためのものです)

var utils = require('./utils'),
  should = require('chai').should(),
  Account = require('../app/model/account');

describe('Account', function() {
  var currentUser = null;
  var account = null;

  it('has created date set on save', function(done) {
    var account = new Account();

    account.save(function(err) {
      account.created.should.not.be.null;
      done();
    });
  });

utils はここから取得されます: http://www.scotchmedia.com/tutorials/express/authentication/1/06

この 1 つのテストだけにとどめておけば、これはうまくいきます。

別のテストを追加して、高速ルートをテストするには、次のようにします。

var request = require('supertest'),
    app = require('../../app'),
    should = require('chai').should();

describe('Account controller', function() {

    it('GET /account returns view', function(done) {
        //omitted for brevity
        done();
    });
});

次に、モデルのテストでタイムアウトエラーが発生します...

それを実行している行はapp = require('../../app')
、それを削除すると、タイムアウトはありません。

私はそれがマングース接続と関係があるかもしれないことを認識していますが、テスト間でそれを「共有」する方法がわかりませんか?

4

1 に答える 1

0

mocha にはルート Suiteがあります。

You may also pick any file and add "root" level hooks, for example add beforeEach() outside of describe()s then the callback will run before any test-case regardless of the file its in. This is because Mocha has a root Suite with no name.

これを使用して、Express サーバーを 1 回起動します (開発サーバーとは異なるポートで実行されるように、環境変数を使用します)。

before(function () {
  process.env.NODE_ENV = 'test';
  require('../../app.js');
});

(require は同期的であるため、ここでは は必要ありませんdone()。) これは、このルートレベルのbefore関数が含まれる異なるテスト ファイルの数に関係なく、サーバーは 1 回だけ起動されるというものでした。

于 2013-11-20T14:43:16.723 に答える