2013-04-11 48 views
2

我有這個功能,我想與摩卡測試:如何使用mocha與createReadStream函數進行鏈測試?

exports.readFile = readFile; 
function readFile(filepath, startOffset, outputStream){ 
    var fileSize = fs.statSync(filepath).size; 
    var length = fileSize - startOffset; 
    console.log(startOffset); 
    fs.createReadStream(filepath, 
        {start: startOffset, end: fileSize} 
             ).pipe(outputStream); 
} 

我用下面的代碼來測試我的功能:

var edp = require("edp.js"); 
    var Buffered = require("buffered-stream"); 
    var sampleData = 'A small test.'; 
fs.writeFileSync('./test.txt', sampleData); 

var filedata = ''; 
var smallBufferedStream = new Buffered(20); 
smallBufferedStream.on("data", function(data){ 
     filedata += data; 
}); 

describe('File content redirection', function(){ 
    describe('reading small file from byte 0', function(){ 
     it('data should be equal', function(done){ 
       filedata = ''; 
       edp.readFile('./test.txt', 0, smallBufferedStream); 
       smallBufferedStream.once('end', function(){ 
        //sampleData value is "A small test. 
        assert.equal(filedata, sampleData); 
        done(); 
       }); 
     }); 
    }); 
    describe('reading small file from byte 8', function(){ 
     it('data should be equal', function(done){ 
       filedata = ''; 
       edp.readFile('./test.txt', 8, smallBufferedStream); 
       smallBufferedStream.once('end', function(){ 
        //sampleData value here is "A small test. 
        //It should be 'test.' 
        assert.equal(filedata, sampleData.substr(8)); 
        done(); 
       }); 
     }); 
    }); 
}); 

當我運行摩卡命令,我得到如下:

0 
․8 
․ 

✖ 1 of 2 tests failed: 

1) File content redirection reading small file from byte 8 data should be equal: 

    actual expected 

    A small test. 

編輯:這個問題是來自未測試

之間重置smallBufferedStream

這隻發生在摩卡(我做了一些外部程序的測試,這工作)。

如何強制我的緩衝流重置一個新的流,每次我在摩卡里面調用它時?

回答

0

不知道這是做到這一點的最佳方式,但我終於發現問題似乎來自函數的異步調用。通過我需要測試值

describe('reading small file from byte 8', function(){ 
    it('data should be equal', function(done){ 
      var smallBufferedStream = new Buffered(20); 
      var filedata = ''; 
      smallBufferedStream.on("data", function(data){ 
        filedata += data; 
      }); 
      var fd = edp.readFile(smallTestFile, 8, smallBufferedStream); 
      smallBufferedStream.once('end', function(){ 
       assert.equal(filedata, sampleData.substr(1)); 

       done(); 
      }); 
    }); 
}); 

我更換8:

我解決了重複我的代碼這樣的問題。

0

您可以在摩卡中使用beforeEach來重置每個測試之間的所有數據。所以你上面的代碼應該通過下列方式工作:

//Have to initialize these here so they are in scope in the describe functions. 
var filedata = null; 
var smallBufferedStream = null; 
describe('File content redirection', function(){ 
    beforeEach(function(done) { 
     filedata = ''; 
     smallBufferedStream = new Buffered(20); 
     smallBufferedStream.on("data", function(data){ 
      filedata += data; 
     }); 
     done() 
    }); 
    describe('reading small file from byte 0', function(){ 
     it('data should be equal', function(done){ 
       filedata = ''; 
       edp.readFile('./test.txt', 0, smallBufferedStream); 
       smallBufferedStream.once('end', function(){ 
        //sampleData value is "A small test. 
        assert.equal(filedata, sampleData); 
        done(); 
       }); 
     }); 
    }); 
    describe('reading small file from byte 8', function(){ 
     it('data should be equal', function(done){ 
       filedata = ''; 
       edp.readFile('./test.txt', 8, smallBufferedStream); 
       smallBufferedStream.once('end', function(){ 
        //sampleData value here is "A small test. 
        //It should be 'test.' 
        assert.equal(filedata, sampleData.substr(8)); 
        done(); 
       }); 
     }); 
    }); 
}); 
相關問題