vendredi 29 janvier 2016

How to mock fs.createReadStream w/ sinon

I am using mocha, supertest and sinon to test this:

stream = fs.createReadStream(somepath);
stream.on('open', function() {
  stream.pipe(res);
});

Here is my test where I am trying to mock the fs.createReadStream() method:

var mockedStream = new stream.Readable();
mockedStream._read = function noop() { };
mockedStream.emit('open');
mockedStream.emit('data', 'mock');
mockedStream.emit('end');
sandbox.mock(fs)
  .expects('createReadStream')
  .returns(mockedStream);

The mock is getting injected correctly, but the open event does not seem to be firing in my code.

Aucun commentaire:

Enregistrer un commentaire