2013-12-10 65 views
2

生病馬上與一些代碼業務:單元測試節點模塊,模塊變量舉止怪異

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

對於那些誰沒看過頭,我試圖做單元測試與nodemocha在這裏。

我想知道我在做什麼錯,爲什麼在調用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 
+0

你可以顯示io.init的代碼嗎? –

+0

正如你所看到的,my.io.js有一個名爲init的方法被導出,我看到我已經修改了這個例子,但是修正了它。 – furier

+0

請你可以在'io.js'中發佈'loadPaths''函數的代碼嗎? – jabclab

回答

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()功能,它可能是一些錯誤,因爲我的重構測試的結果。