dimanche 8 mars 2015

How to mock a glob call in Node.js

I'm using Mocha, Chai and Sinon JS to write Unit Tests for my Node.js application.


Here is the module I want to test:



var glob = require('glob');
var pluginInstaller = require('./pluginInstaller');

module.exports = function(app, callback) {
'use strict';

if (!app) {
return callback(new Error('App is not defined'));
}

glob('./plugins/**/plugin.js', function(err, files) {
if (err) {
return callback(err);
}

pluginInstaller(files, callback, app);
});
};


I have a test for the case, when there is no app using .to.throw(Error).


But I have no idea how to mock the glob call. In particular I want to tell my Test-Method, what the glob-call returns and then to check, if the pluginInstaller has been called, using sinon.spy.


Here is my test I have so far:



var expect = require('chai').expect,
pluginLoader = require('../lib/pluginLoader');

describe('loading the plugins', function() {
'use strict';

it ('returns an error with no app', function() {
expect(function() {
pluginLoader(null);
}).to.throw(Error);
});
});

Aucun commentaire:

Enregistrer un commentaire