如何使用createReadStream函数与mocha进行链式测试?

时间:2013-04-11 00:21:14

标签: javascript node.js mocha

我有这个函数我想用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);
}

我使用以下代码来测试我的功能:

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.

编辑:问题来自于未在测试之间重置的smallBufferedStream

这只发生在mocha中(我在外部程序上进行了一些测试,这很有效)。

每次在mocha中调用时,如何强制缓冲流重置新流?

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分: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();
                });
        });
    });
});