如何使用createReadStream函数链接testing与摩卡?

我有这个function,我想用摩卡testing:

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

我使用下面的代码来testing我的function:

 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. 

编辑:问题来自testing之间不重置的smallBufferedStream

这只发生在摩卡(我做了一些外部程序的testing,这工作)。

我怎么能强制我的缓冲stream重置一个新的stream,每次我在摩卡里面调用它?

不知道这是做到这一点的最好方法,但我终于发现问题似乎来自function的asynchronous调用。

我解决了像这样重复我的代码的问题:

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

我用我需要testing的值replace8。

您可以使用摩卡上的beforeEach来重置每个testing之间的所有数据。 所以你上面的代码应该通过下面的工作:

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