I am really new to mocha/chai/sinon, and testing in general. I've managed to successfully test a basic express server, a function returning promises, and a basic sequelize setup to get my nose wet; but I am stuck on spies/stubs/mocks.
My fist hiccup is trying to check that glob has been called in an external module:
//in utils.js
var glob = require('glob');
module.exports = {
funToTest: function (msg, callback) {
console.log(msg);
glob('*md', {
cwd: 'files/'
}, function (err, files) {
console.log(files);
});
callback();
callback();
}
};
Using a mocha/chai/sinon/sinon-chai combination:
// in utils-test.js
var utils = require('utils.js');
var glob = require('glob');
describe('Utils', function () {
describe('funToTest method', function () {
const callback = sinon.spy();
const globSpy = sinon.spy(glob);
before(function (done) {
utils.funToTest('Files:', callback);
done();
});
// This PASSES fine
it ('should call our callback twice', function () {
expect(callback).to.have.been.calledTwice;
});
// This NOT SO MUCH
it('should call glob once', function () {
expect(globSpy).to.have.been.calledOnce;
});
)};
)};
The above fails with an assertion error:
AssertionError: expected glob to have been called exactly once, but it was called 0 times
So how do I spy on the glob dependency in utils.funToTest to see if gets called?
Thanks as ever for any pointers...
Aucun commentaire:
Enregistrer un commentaire