Javascript 带mocha的单元测试节点模块,模块变量动作奇怪

Javascript 带mocha的单元测试节点模块,模块变量动作奇怪,javascript,node.js,mocha.js,Javascript,Node.js,Mocha.js,我会用一些代码来做生意: hangman.io.js: var from = require('fromjs'), fs = require('fs'); var defaultBasePath = 'lib/hangman/wordlists'; var filePaths = []; function init(basePath) { basePath = basePath || defaultBasePath; filePaths = loadPaths(bas

我会用一些代码来做生意:

hangman.io.js:

var from = require('fromjs'),
    fs = require('fs');

var defaultBasePath = 'lib/hangman/wordlists';
var filePaths = [];

function init(basePath) {
    basePath = basePath || defaultBasePath;
    filePaths = loadPaths(basePath);
}

function loadPaths(basePath) {
    var wordLists = fs.readdirSync(basePath);
    return from(wordLists).select(function (x) {
        return basePath + '/' + x;
    }).toArray();
}

function getFilePath(type) {
    if (!filePaths || 
        !(filePaths instanceof Array) || 
        !(filePaths.length > 0)) throw new Error('No file paths registered.');
    ...
}

module.exports = {
    init: init,
    getFilePath: getFilePath
}
var io = require('../hangman.io'),
    should = require('should');

describe('io', function () {
    before(function () {
        io.init();
    });
    describe('getLineCount(path)', function () {
        var path = io.getFilePath('test'); //<== this lines throws the exception 
        //"No file paths registered", even tho I have called init() on io.
        var count = io.getLineCount(path);
        count.should.be.an.Number;
        count.should.be.eql(4);
    });
});
var io = require('../hangman.io'),
    should = require('should');

describe('io', function () {
    before(function () {
        io.init();
    });
    describe('getLineCount(path)', function () {
        it('should return a line count of 4 when type is "test"', function(){
            var path = io.getFilePath('test'); //<== this lines throws the exception 
            //"No file paths registered", even tho I have called init() on io.
            var count = io.getLineCount(path);
            count.should.be.an.Number;
            count.should.be.eql(4);
        })
    });
});
hangman.io.tests.js:

var from = require('fromjs'),
    fs = require('fs');

var defaultBasePath = 'lib/hangman/wordlists';
var filePaths = [];

function init(basePath) {
    basePath = basePath || defaultBasePath;
    filePaths = loadPaths(basePath);
}

function loadPaths(basePath) {
    var wordLists = fs.readdirSync(basePath);
    return from(wordLists).select(function (x) {
        return basePath + '/' + x;
    }).toArray();
}

function getFilePath(type) {
    if (!filePaths || 
        !(filePaths instanceof Array) || 
        !(filePaths.length > 0)) throw new Error('No file paths registered.');
    ...
}

module.exports = {
    init: init,
    getFilePath: getFilePath
}
var io = require('../hangman.io'),
    should = require('should');

describe('io', function () {
    before(function () {
        io.init();
    });
    describe('getLineCount(path)', function () {
        var path = io.getFilePath('test'); //<== this lines throws the exception 
        //"No file paths registered", even tho I have called init() on io.
        var count = io.getLineCount(path);
        count.should.be.an.Number;
        count.should.be.eql(4);
    });
});
var io = require('../hangman.io'),
    should = require('should');

describe('io', function () {
    before(function () {
        io.init();
    });
    describe('getLineCount(path)', function () {
        it('should return a line count of 4 when type is "test"', function(){
            var path = io.getFilePath('test'); //<== this lines throws the exception 
            //"No file paths registered", even tho I have called init() on io.
            var count = io.getLineCount(path);
            count.should.be.an.Number;
            count.should.be.eql(4);
        })
    });
});

好吧,我很抱歉对这件事大惊小怪。这件事已经纠缠了很长时间了。我想我有点沮丧和压力

不管怎么说,这是我纠正错误的“解决方案”

hangman.io.tests.js:

var from = require('fromjs'),
    fs = require('fs');

var defaultBasePath = 'lib/hangman/wordlists';
var filePaths = [];

function init(basePath) {
    basePath = basePath || defaultBasePath;
    filePaths = loadPaths(basePath);
}

function loadPaths(basePath) {
    var wordLists = fs.readdirSync(basePath);
    return from(wordLists).select(function (x) {
        return basePath + '/' + x;
    }).toArray();
}

function getFilePath(type) {
    if (!filePaths || 
        !(filePaths instanceof Array) || 
        !(filePaths.length > 0)) throw new Error('No file paths registered.');
    ...
}

module.exports = {
    init: init,
    getFilePath: getFilePath
}
var io = require('../hangman.io'),
    should = require('should');

describe('io', function () {
    before(function () {
        io.init();
    });
    describe('getLineCount(path)', function () {
        var path = io.getFilePath('test'); //<== this lines throws the exception 
        //"No file paths registered", even tho I have called init() on io.
        var count = io.getLineCount(path);
        count.should.be.an.Number;
        count.should.be.eql(4);
    });
});
var io = require('../hangman.io'),
    should = require('should');

describe('io', function () {
    before(function () {
        io.init();
    });
    describe('getLineCount(path)', function () {
        it('should return a line count of 4 when type is "test"', function(){
            var path = io.getFilePath('test'); //<== this lines throws the exception 
            //"No file paths registered", even tho I have called init() on io.
            var count = io.getLineCount(path);
            count.should.be.an.Number;
            count.should.be.eql(4);
        })
    });
});

在此错误之前,我进行了大量的单元测试,没有丢失
it()
函数,这可能是重构我的测试所导致的一些错误。

可以显示io.init的代码吗?正如您所看到的,my.io.js有一个名为init的方法,该方法已导出,我看到我对示例做了一些修改,我会修复它。请你在
io.js
中发布
加载路径
函数的代码好吗?我知道fromjs库没有bug,用
加载路径(基本路径)
函数更新了问题好吧,我在这里发现了bug,是我忘记了使用
它('should blabla…',callback);
description()函数中。