摩卡unit testing节点模块,模块variables奇怪

生病了一些代码的业务:

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

对于那些没有阅读头文件的人,我试图在这里用节点摩卡进行unit testing。

我想知道我在做什么错了,为什么不调用io.init()后填充path的variables。 我使用WebStorm ,如果我添加断点和debugging代码,我可以清楚地看到数组正在被填充。 但之后,当我调用io.getLineCount(path)函数时,filePathsvariables是空的,我确保你没有其他代码在幕后进行操作。

我只是没有得到这个,这是一个错误,或者我做错了什么,或者我只是一个白痴?

我也尝试在unit testing本身内移动io.init()函数,也没有不同的行为。

这里是一个带有修改path的堆栈跟踪,但是其他一切都是原创的。

 "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 

好的,我很抱歉为此做了一个大惊小怪的事情。 刚刚被这个相当长一段时间困扰着。 猜猜我有点沮丧和强调。

反正这里是“解决scheme”,纠正了我的错误。

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()函数中缺less此行。

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

在这个错误之前,我有很多的unit testing不会错过it()函数,可能是由于重构我的testing而导致的一些错误。