node.js - How to simulate error returned from fs.readFile for testing purposes? -


i new test-driven development , trying develop automated testing suite application.

i have written tests verify data received successful call node's fs.readfile method, see in screenshot below, when test coverage istanbul module correctly displays have not tested case error returned fs.readfile.

enter image description here

how can this? have hunch have mock file-system, have tried using mock-fs module, haven't succeeded. path file hard-coded in function, , using rewire call unexported function application code. therefore, when use rewire's getter method access getappstatus function, uses real fs module used in async.js file getappstatus resides.

here's code testing:

// check whether application turned on function getappstatus(cb){   fs.readfile(directory + '../config/status.js','utf8', function(err, data){     if(err){       cb(err);     }     else{       status = data;       cb(null, status);     }   }); } 

here's test have written case data returned:

  it('application should either on or off', function(done) {       getappstatus(function(err, data){         data.should.eq('on' || 'off')          done();       })   }); 

i using chai assertion library , running tests mocha.

any in allowing me simulate error being returned fs.readfile can write test case scenario appreciated.

the better use mock-fs, if provide no file, return enoent. careful call restore after test avoid impact on other tests.

add @ beginning

  var mock = require('mock-fs'); 

and test

before(function() {   mock(); }); it('should throw error', function(done) {   getappstatus(function(err, data){     err.should.be.an.instanceof(error);     done();   }); }); after(function() {   mock.restore(); }); 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -