我有這個功能,我想與摩卡測試:如何使用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這隻發生在摩卡(我做了一些外部程序的測試,這工作)。
如何強制我的緩衝流重置一個新的流,每次我在摩卡里面調用它時?