Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用带有createReadStream函数的mocha进行链测试?_Javascript_Node.js_Mocha.js - Fatal编程技术网

Javascript 如何使用带有createReadStream函数的mocha进行链测试?

Javascript 如何使用带有createReadStream函数的mocha进行链测试?,javascript,node.js,mocha.js,Javascript,Node.js,Mocha.js,我想用摩卡测试一下这个功能: exports.readFile = readFile; function readFile(filepath, startOffset, outputStream){ var fileSize = fs.statSync(filepath).size; var length = fileSize - startOffset; console.log(startOffset); fs.createReadStream(filepath

我想用摩卡测试一下这个功能:

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();
                });
        });
    });
});
当我运行mocha命令时,我获得以下信息:

 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.
//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();
                });
        });
    });
});
编辑:问题来自smallBufferedStream,它在测试之间未重置

这只发生在摩卡(我在一个外部程序上做了一些测试,效果很好)


如何在每次在mocha内部调用缓冲流时强制其重置新流?

不确定这是最好的方法,但我最终发现问题似乎来自函数的异步调用

我解决了复制代码的问题,如下所示:

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替换为需要测试的值。

您可以在mocha中使用beforeach重置每次测试之间的所有数据。因此,上面的代码应该通过执行以下操作来工作:

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