单元测试节点模块与摩卡,模块变量行为奇怪

时间:2013-12-10 13:11:19

标签: javascript node.js mocha

生病与某些代码开展业务:

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
}

hangman.io.tests.js:

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

对于那些没有阅读标题的人,我正在尝试使用 node mocha 进行单元测试。

我想知道我做错了什么,为什么在调用io.init()之后变量没有填充路径。 我使用 WebStorm ,如果我添加断点并调试代码,我可以清楚地看到数组正在填充。 但后来当我突然调用io.getLineCount(path)函数时,filePaths变量为空, 我确保你没有其他代码进入幕后并操纵变量。

我只是不明白,这是一个错误,还是我做错了什么,还是我只是个白痴?

我也尝试在单元测试中移动io.init()函数,也没有不同的行为。

这是一个带有修改路径的堆栈跟踪,但其他所有内容都是原始的。

"C:\Program Files (x86)\nodejs\node.exe" C:\Github\wolfram\node_modules\mocha\bin\_mocha --recursive --timeout 0 --ui bdd --reporter "C:\Program Files (x86)\JetBrains\WebStorm 7.0.2\plugins\NodeJS\js\mocha\mochaIntellijReporter.js" C:\Github\wolfram\test
Testing started at 13:48 ...

C:\Github\wolfram\lib\hangman\hangman.io.js:28
 || !(filePaths instanceof Array) || !(filePaths.length > 0)) throw new Error(
                                                                    ^
Error: No file paths registered.
    at Object.getFilePath (C:\Github\wolfram\lib\hangman\hangman.io.js:28:91)
    at Suite.<anonymous> (C:\Github\wolfram\test\hangman\hangman.io.tests.js:52:27)
    at context.describe.context.context (C:\Github\wolfram\node_modules\mocha\lib\interfaces\bdd.js:73:10)
    at Suite.<anonymous> (C:\Github\wolfram\test\hangman\hangman.io.tests.js:51:9)
    at context.describe.context.context (C:\Github\wolfram\node_modules\mocha\lib\interfaces\bdd.js:73:10)
    at Suite.<anonymous> (C:\Github\wolfram\test\hangman\hangman.io.tests.js:32:5)
    at context.describe.context.context (C:\Github\wolfram\node_modules\mocha\lib\interfaces\bdd.js:73:10)
    at Object.<anonymous> (C:\Github\wolfram\test\hangman\hangman.io.tests.js:9:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at C:\Github\wolfram\node_modules\mocha\lib\mocha.js:157:27
    at Array.forEach (native)
    at Mocha.loadFiles (C:\Github\wolfram\node_modules\mocha\lib\mocha.js:154:14)
    at Mocha.run (C:\Github\wolfram\node_modules\mocha\lib\mocha.js:341:31)
    at Object.<anonymous> (C:\Github\wolfram\node_modules\mocha\bin\_mocha:351:7)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

Process finished with exit code 8

1 个答案:

答案 0 :(得分:0)

好的,我很抱歉对此大做文章。现在已经有一段时间了。猜猜我有点沮丧和压力。

无论如何这里是我的错误纠正的“解决方案”。

<强> hangman.io.tests.js:

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

description()函数中缺少此行。

The it('should blabla when bla', function(){
    //put stuff to test in here
});

在此错误之前我进行了大量的单元测试而没有错过it()函数,因为重构我的测试可能会出现一些错误。