mocha を使用してすべてのテストを実行する前に、mongoose との接続 (およびデータベースのクリーンアップ) を作成しています (_setup.js):
var mongoose = require('mongoose'),
nconf = require('nconf');
nconf.env().argv();
var _conn;
before(function(done){
_conn = mongoose.createConnection(nconf.get('TEST_DB'), function(error){
if(error) return done(error);
_conn.db.dropDatabase(done);
});
});
after(function(done){
_conn.db.dropDatabase(function(error){
if(error) return done(error);
_conn.close(done);
});
});
他のテスト スイートでは、マングース モデルを構築するためにこの接続が必要です。これらのテストはmocha js APIを介してアプリで実行できるため、デフォルトのmongoose接続の代わりに別の接続を使用しています。アプリはデフォルトのマングース接続を使用します。接続変数が必要なテストの例:
var should = require('should'),
service = require('../lib/service')(_conn); // << somehow need that conn variable created in before tests
describe('Service', function(){
describe('#dodboperation()', function(){
//tests and stuff
モカのファイル/テストスイート間で変数を渡す方法はありますか? 提案?