2

I have this function I would like to test with mocha:

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

I use the following code to test my 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();
                });
        });
    });
});

When I run the mocha command, I obtain the following:

 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.

EDIT: the problem comes from the smallBufferedStream which is not reset between tests

This only happens in mocha (I made some test on an external program and this works).

How can I force my buffered stream to reset a new stream each time I call it inside mocha ?

4

2 回答 2

0

您可以在 mocha 中使用 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();
                });
        });
    });
});
于 2014-09-17T05:07:22.303 回答
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 替换为我需要测试的值。

于 2013-04-29T13:38:17.437 回答