模块的Mocha测试需要一些选项或参数

时间:2017-11-10 13:50:27

标签: javascript node.js mocha chai

我有一个名为 us_sea_public 的模块,它通过 app.js 运行,这是我的主文件。 通常,我必须将几个参数(readPost,us_sea,public和db)传递给app.js以运行us_sea_public,如下所示:

node app.js --readPost us_sea --public --db mongodb://localhost:27017/US_SEA

现在,我想为这个模块创建一个mocha测试(us_sea_public)但是我不知道如何实现测试。我有我测试的样本结构。

var publicParse = require('./app.js');
var expect = require("chai").expect;
describe('publicParse', function () {
    it('parseStandard', function (done) {
    var result = publicParse. //??!!!
//I dont know how to call the us_sea_public through app.js to produce result
    expect(result.rules[0].byweekday).to.eql(["MO", "TU", "WE", "TH"]);
    expect(result.rules[0].tstart.getHours()).to.equal(16);
    done();
  });
});

在package.json中:

"scripts": {
"start": "node app.js",
"test": "mocha tests/testUtilsUS_SEA_public.js"
}

app.js导出us_sea_public,如下所示:

// load modules from modules folder
var modules = {};
var osmModules = {};

glob("./modules/*_*_*.js", function (er, files) {
files.forEach(function (moduleFile) {
    var module = require(moduleFile);
    var info = module.info;
    moduleFile = moduleFile.substring(moduleFile.lastIndexOf('/') + 1);
        modules[info.country.toLowerCase() + "_" + info.city.toLowerCase()].push({
            module: module,
            info: info
        });
    }

});
// call the actual init function
init();
});

 /**
* this will actuall "run" a module
* @param program the program for global variables
* @param module the module to run
* @param done called when finished
* @param datafile optional datafile, this will be passed to the run function
*/
function run(program, module, done, datafile) {
activeModules++;
totalModules++;

if (!db) {
    db = mongoskin.db(program.db);
}
// run the module
if (program.readPostgres)
      module.readPostgres(true);
    module.run(db, done);  //here is the place where I call my us_sea_public

}


/**
* Run modules
* @param program the program object to get confiuration from
* @param countryCode the current country code
* @param moduleList the list of modules to run
*/
function runModules(program, countryCode, moduleList) {
run(program, mod.module, exitApp, targetFile);
}

function getOpts() {
var opts = {};
process.argv.forEach(function (args) {
    if (args.indexOf('--') !== -1) {
        opts[args.substring(2)] = true;
    }
});

return opts;
}
var opts = getOpts();
function runAction() {
// check opts
var country,
moduleList;

// check "pre"-opts
var found;
// parameter index
var pidx = 2;
do {
    found = false;
    country = process.argv[pidx++];
    switch (country) {
    case '--db':
        program.db = process.argv[pidx++];
        found = true;
        break;
    case '--readPostgres':
        program.readPostgres = true;
        found = true;
        break;
    }
} while (found);


    moduleList = [];
    //  console.log("   ---log    "+ JSON.stringify(modules[country]));
    modules[country].forEach(function (mod) {
        if (opts[mod.info.name]) {
            moduleList.push(mod);
        }
    });

    console.log(country + moduleList)
    runModules(program, country, moduleList);

 };


/**
* parameter parsing and execution
*/
function init() {
/* **********************************
 *  command line parameter and usage
 */
program
.version('1.1.0')
.option('--db [url]', 'mongo database connection url (default: 
mongodb://localhost:27019/pb)', 'mongodb://localhost:27019/pb')
.option('--readPost', 'read from postgresql database connection url in 
geobox (postgres://postgres:postgres@localhost:5433/City_name)')

var skipAll = false;
// now add sub-commands for all modules
for (var countryCode in modules) {
    var moduleList = modules[countryCode];
    var curcommand = program.command(countryCode).description('City: ' + 
  countryCode);

    var next = curcommand
        .option('--all', 'call all modules for the ' + countryCode);
    moduleList.forEach(function (mod) {
        next = next.option('--' + mod.info.name, mod.info.description);
    });
    next.action(function (cmd, options) {
        if (!process.argv.slice(3).length) {
            cmd.help();
            return;
        }
        runAction();
        skipAll = true;
    });
}

if (!process.argv.slice(2).length) {
    program.outputHelp();
    return;
}

program.parse(process.argv);

}

和us_sea_public看起来像:

exports.run = function (db, done) {}

0 个答案:

没有答案