简单的摩卡测试设置

时间:2015-05-07 17:55:07

标签: node.js mocha

我使用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框架,但我能在nconf函数中定义it() var的唯一方法是在before()函数之外定义它。这是最好的方法吗?

1 个答案:

答案 0 :(得分:3)

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

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
    });
});