Node.js 简单的摩卡测试设置

Node.js 简单的摩卡测试设置,node.js,mocha.js,Node.js,Mocha.js,我正在使用Mocha测试Node.js命令行应用程序: describe('#call', function () { var nconf = require('nconf'); //is this the best place for this? before(function () { //var nconf = require('nconf'); i'd rather define it here nconf

我正在使用Mocha测试Node.js命令行应用程序:

 describe('#call', function () {
        var nconf = require('nconf'); //is this the best place for this?
        before(function () {
            //var nconf = require('nconf'); i'd rather define it here
            nconf.use('memory');
            nconf.set('fp','data_for_testing/csvfile.csv');
            nconf.set('mptp','map_ivr_itg');  
            nconf.set('NODE_ENV','dev_local');
        });
        it('should run without throwing an error or timing out', function (done) {
            var start = require('../lib/setup');
            start.forTesting(done);
            start.run(nconf); //need nconf to be defined here
        });
    });

我想正确地使用Mocha框架,但是我能得到
it()
函数中定义的
nconf
变量的唯一方法是在
before()函数之外定义它。最好的方法是什么?

正如Yury Tarabanko在评论中所说,最好的方法是在before()之外创建nconf变量,并在每次运行before时重新分配它

describe('#call', function () {
    var nconf = null; // scope of variable is the whole #call tests

    before(function () {
        nconf = require('nconf'); // will reassign before each test
        nconf.use('memory');
        nconf.set('fp','data_for_testing/csvfile.csv');
        nconf.set('mptp','map_ivr_itg');  
        nconf.set('NODE_ENV','dev_local');
    });

    it('should run without throwing an error or timing out', function (done) {
        var start = require('../lib/setup');
        start.forTesting(done);
        start.run(nconf); // nconf is in scope
    });
}); 

正如Yury Tarabanko在评论中所说,最好的方法是在before()之外创建nconf变量,并在每次运行before()时重新分配它

describe('#call', function () {
    var nconf = null; // scope of variable is the whole #call tests

    before(function () {
        nconf = require('nconf'); // will reassign before each test
        nconf.use('memory');
        nconf.set('fp','data_for_testing/csvfile.csv');
        nconf.set('mptp','map_ivr_itg');  
        nconf.set('NODE_ENV','dev_local');
    });

    it('should run without throwing an error or timing out', function (done) {
        var start = require('../lib/setup');
        start.forTesting(done);
        start.run(nconf); // nconf is in scope
    });
}); 

正如Yury Tarabanko在评论中所说,最好的方法是在before()之外创建nconf变量,并在每次运行before()时重新分配它

describe('#call', function () {
    var nconf = null; // scope of variable is the whole #call tests

    before(function () {
        nconf = require('nconf'); // will reassign before each test
        nconf.use('memory');
        nconf.set('fp','data_for_testing/csvfile.csv');
        nconf.set('mptp','map_ivr_itg');  
        nconf.set('NODE_ENV','dev_local');
    });

    it('should run without throwing an error or timing out', function (done) {
        var start = require('../lib/setup');
        start.forTesting(done);
        start.run(nconf); // nconf is in scope
    });
}); 

正如Yury Tarabanko在评论中所说,最好的方法是在before()之外创建nconf变量,并在每次运行before()时重新分配它

describe('#call', function () {
    var nconf = null; // scope of variable is the whole #call tests

    before(function () {
        nconf = require('nconf'); // will reassign before each test
        nconf.use('memory');
        nconf.set('fp','data_for_testing/csvfile.csv');
        nconf.set('mptp','map_ivr_itg');  
        nconf.set('NODE_ENV','dev_local');
    });

    it('should run without throwing an error or timing out', function (done) {
        var start = require('../lib/setup');
        start.forTesting(done);
        start.run(nconf); // nconf is in scope
    });
}); 

var nconf=null;before(function(){nconf=require('nconf');nconf.use('memory');})^^作为一个代码格式的答案看起来更可读:)您说希望在调用之前在
中定义它,但没有指定任何原因。如果没有令人信服的理由让您在
之前的
中调用
require
,那么这完全是一个意见问题。我组织我的Mocha文件,以便每个文件测试一个库。在这个组织中,除了在顶部调用以外,在任何地方调用
require
调用以及所有其他调用都没有明显的好处。
var nconf=null;before(function(){nconf=require('nconf');nconf.use('memory');})^^作为一个代码格式的答案看起来更可读:)您说希望在调用之前在
中定义它,但没有指定任何原因。如果没有令人信服的理由让您在
之前的
中调用
require
,那么这完全是一个意见问题。我组织我的Mocha文件,以便每个文件测试一个库。在这个组织中,除了在顶部调用以外,在任何地方调用
require
调用以及所有其他调用都没有明显的好处。
var nconf=null;before(function(){nconf=require('nconf');nconf.use('memory');})^^作为一个代码格式的答案看起来更可读:)您说希望在调用之前在
中定义它,但没有指定任何原因。如果没有令人信服的理由让您在
之前的
中调用
require
,那么这完全是一个意见问题。我组织我的Mocha文件,以便每个文件测试一个库。在这个组织中,除了在顶部调用以外,在任何地方调用
require
调用以及所有其他调用都没有明显的好处。
var nconf=null;before(function(){nconf=require('nconf');nconf.use('memory');})^^作为一个代码格式的答案看起来更可读:)您说希望在调用之前在
中定义它,但没有指定任何原因。如果没有令人信服的理由让您在
之前的
中调用
require
,那么这完全是一个意见问题。我组织我的Mocha文件,以便每个文件测试一个库。在这个组织中,除了在顶部以外的任何地方调用
require
调用以及所有其他调用都没有明显的好处。