断言具有相同内容的文件

hel*_*boy 11 mocha.js node.js should.js

我正在使用mocha/supertest/should.js来测试我的Rest Service

GET /files/<hash> 将文件作为流返回.

如何在should.js中断言文件内容是否相同?

it('should return file as stream', function (done) {
    var writeStream = fs.createWriteStream('test/fixtures/tmp.json');

    var req = api.get('/files/676dfg1430af3595');
    req.on('end', function(){
       var tmpBuf = fs.readFileSync('test/fixtures/tmp.json');
       var testBuf = fs.readFileSync('test/fixtures/test.json');

       // How to assert with should.js file contents are the same (tmpBuf == testBuf )
       // ...

       done();
    });
});
Run Code Online (Sandbox Code Playgroud)

Max*_*Max 8

令人惊讶的是,没有人建议使用Buffer.equals。自v0.11以来,这似乎是最快,最简单的方法。

所以你的代码将成为 tmpBuf.equals(testBuf)