2013-01-14 24 views
0
describe 'TheObject', -> 
    before -> 
    console.log 'loading text' 
    fs.readFile('../data/data.json', 'utf8', (err, data) -> 
     text = data 
    ) 
    describe 'simple', -> 
    it 'should pass a simple test', -> 
     b = "1" 
     b.should.equal "1" 

我想使得我的測試不會運行,直到before操作中讀取的文件完成爲止。但這是異步的世界,所以我認爲它的行爲如預期。我可以將所有其他測試以某種方式放入回調中嗎?我可以強制readFile被阻止嗎?如何讓我的'before'函數使其他測試等待其完成?

回答

1

當異步,您before功能應該接受它調用時,它的完成處理的done回調參數:

describe 'TheObject', -> 
    before (done) -> 
    console.log 'loading text' 
    fs.readFile('../data/data.json', 'utf8', (err, data) -> 
     text = data 
     done() 
    ) 
+0

我加的是()完成向READFILE通話和測試,看起來像'描述'測試數據', - > '應該存在', - > text.should.not.equal(null)'但它仍然失敗:文本未定義。 – jcollum

+0

這樣做,謝謝! – jcollum

相關問題