我要用一些代码来做生意:
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);
});
});对于那些没有读标题的人,我在这里尝试用节点和摩卡咖啡进行单元测试。
我想知道我做错了什么,为什么在调用io.init()之后,变量中没有装满路径。我使用WebStorm,如果我添加断点并调试代码,我可以清楚地看到数组正在被填充。但是之后,当我突然调用io.getLineCount(path)函数时,filePaths变量是空的,并且我确保没有其他代码在幕后进入并操作变量。
我只是不明白,这是一个错误,还是我做错了什么,还是我只是个白痴?
我还尝试将io.init()函数移动到单元测试本身中,也没有任何不同的行为。
这里是一个带有修改路径的堆栈跟踪,但其他都是ofc原始的。
"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发布于 2013-12-10 14:03:11
好的,我很抱歉为此大惊小怪。只是一直在纠结这件事很久了。我想我有点沮丧和压力。
无论如何,这里是我的错误纠正的“解决方案”。
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()函数,这可能是重构测试的结果而产生的错误。
https://stackoverflow.com/questions/20495496
复制相似问题