I'm using Mocha and Chai with a node project and wondering how do I test the error callback in node functions?
Here is an example of my code that I want to test:
savePlayer: function(player) {
var playerName = player.name;
modules.fs.writeFile('./Data/' + playerName + '.json', JSON.stringify(player), function (err) {
if (err) {
return console.log(err.message);
}
});
}
This is my test:
describe("savePlayer", function() {
it("Should save the player in JSON, using thier name", function() {
var player = {name: "test" }
modules.data.savePlayer(player);
var playerFile = modules.fs.readFileSync('Data/test.json').toString('utf8');
expect(playerFile).should.exist;
});
});
This passes, but I want full code coverage. This line return console.log(err.message); is untested and I'm unsure how to fake an error and test that the console reported an error.
savePlayerfunction