1.0.8 • Published 5 years ago
nobs-ioc v1.0.8
DEPRECATION / PENDING DELETION NOTICE
NOTICE nobs-ioc IS DEPRECATED AND WILL BE REMOVED FROM THE NPM REGISTRY ON 25 APRIL 2020.
https://bitbucket.org/tonymke/nobs-ioc/issues/1/deprecation-pending-removal-notice
nobs-ioc
No-bullshit IOC for JavaScript.
API
- ioc.require(string)
- pass a corresponding module name or path, as you would to normal require
- if something is registered to return on that string (e.g. a mock), it will return. Otherwise, the module is returned.
- ioc.dispenseForModule(string, anything)
- register an item to return when a module with the name of string is requested.
- ioc.clearDispenser()
- remove all module registrations
Example
unit.js
var ioc = require('nobs-ioc');
var fs = ioc.require('fs');
var unit = function(directory) {
return fs.readdirSync(directory);
};
module.exports = unit;
mocha_test_for_unit.js
var ioc = require('nobs-ioc');
var chai = require('chai');
var sinon = require('sinon');
var expect = chai.expect;
var assert = chai.assert;
describe('unit', function() {
beforeEach(function() {
var mockFs = {
readdirSync: function(){}
};
var stub = sinon.stub(mockFs, 'readdirSync');
stub
.withArgs(sinon.match('./directory'))
.returns(['someFile.js']);
stub
.withArgs(sinon.match(function(value) {
return value !== './directory';
}))
.throws(new Error('Unexpected call to fs.readdirSync'));
ioc.dispenseForModule('fs', mockFs);
});
afterEach(function() {
ioc.clearDispenser();
});
it('should return the results of fs.readdirSync on its input', function() {
var unit = require('./unit.js');
var result = new unit('./directory');
expect(Array.isArray(result)).to.be.true;
expect(result.length).to.equal(1);
expect(result).to.include('someFile.js');
});
it('should throw when its input is not \'./directory\'', function() {
var unit = require('./unit.js');
expect(function() {
return unit('fail');
}).to.throw();
});
it('should clear its dispenser when called', function() {
ioc.clearDispenser();
expect(ioc.require('fs')).to.equal(require('fs'));
});
});
Author
License
MIT-0