Skip to content
Browse files

Add support for arrays of allowable modules.

  • Loading branch information...
1 parent 3c57ec4 commit 126024e88512a86912fd2568ec95ae96ce1af06f @mfncooper committed Sep 16, 2012
Showing with 91 additions and 0 deletions.
  1. +9 −0 README.md
  2. +24 −0 mockery.js
  3. +5 −0 test/fixtures/fake_module_2.js
  4. +53 −0 test/mockery.functional.js
View
9 README.md
@@ -108,6 +108,15 @@ again, you can deregister it if you need to:
mockery.deregisterAllowable('./my-source-under-test');
+Sometimes you'll find that you need to register several modules at once. A
+convenience function lets you do this with a single call:
+
+ mockery.registerAllowables(['async', 'path', 'util']);
+
+and similarly to deregister several modules at once, as you would expect:
+
+ mockery.deregisterAllowables(['async', 'path', 'util']);
+
### Unhooking
By default, the Node module loader will load a given module only once, caching
View
24 mockery.js
@@ -202,6 +202,17 @@ function registerAllowable(mod, unhook) {
}
/*
+ * Register an array of modules as 'allowed'. This is a convenience function
+ * that performs the same function as 'registerAllowable' but for an array of
+ * modules rather than a single module.
+ */
+function registerAllowables(mods, unhook) {
+ mods.forEach(function (mod) {
+ registerAllowable(mod, unhook);
+ });
+}
+
+/*
* Deregister a module as 'allowed'. A subsequent 'require' for that module
* will generate a warning that the module is not allowed, unless or until a
* mock or substitute is registered for that module.
@@ -219,6 +230,17 @@ function deregisterAllowable(mod) {
}
/*
+ * Deregister an array of modules as 'allowed'. This is a convenience function
+ * that performs the same function as 'deregisterAllowable' but for an array of
+ * modules rather than a single module.
+ */
+function deregisterAllowables(mods) {
+ mods.forEach(function (mod) {
+ deregisterAllowable(mod);
+ });
+}
+
+/*
* Deregister all mocks, substitutes, and allowed modules, resetting the state
* to a clean slate. This does not affect the enabled / disabled state of
* mockery, though.
@@ -245,7 +267,9 @@ exports.warnOnUnregistered = warnOnUnregistered;
exports.registerMock = registerMock;
exports.registerSubstitute = registerSubstitute;
exports.registerAllowable = registerAllowable;
+exports.registerAllowables = registerAllowables;
exports.deregisterMock = deregisterMock;
exports.deregisterSubstitute = deregisterSubstitute;
exports.deregisterAllowable = deregisterAllowable;
+exports.deregisterAllowables = deregisterAllowables;
exports.deregisterAll = deregisterAll;
View
5 test/fixtures/fake_module_2.js
@@ -0,0 +1,5 @@
+var bar = function() {
+ return 'real bar';
+};
+
+exports.bar = bar;
View
53 test/mockery.functional.js
@@ -137,6 +137,59 @@ module.exports = testCase({
})
}),
+ "when an array of allowables is registered": testCase({
+ setUp: function (callback) {
+ mockery.registerAllowables(
+ ['./fixtures/fake_module', './fixtures/fake_module_2']);
+ callback();
+ },
+
+ "and mockery is enabled": testCase({
+ setUp: function (callback) {
+ mockery.enable();
+ callback();
+ },
+
+ "requiring the modules causes no warning to be logged": function (test) {
+ var mock_console = sinon.mock(console);
+ mock_console.expects('warn').never();
+
+ var fake_module = require('./fixtures/fake_module');
+ test.equal(fake_module.foo(), 'real foo');
+
+ var fake_module_2 = require('./fixtures/fake_module_2');
+ test.equal(fake_module_2.bar(), 'real bar');
+
+ mock_console.verify();
+ mock_console.restore();
+ test.done();
+ },
+
+ "and the allowables are deregistered": testCase({
+ setUp: function (callback) {
+ mockery.deregisterAllowables(
+ ['./fixtures/fake_module', './fixtures/fake_module_2']);
+ callback();
+ },
+
+ "requiring the modules causes warnings to be logged": function (test) {
+ var mock_console = sinon.mock(console);
+ mock_console.expects('warn').twice();
+
+ var fake_module = require('./fixtures/fake_module');
+ test.equal(fake_module.foo(), 'real foo');
+
+ var fake_module_2 = require('./fixtures/fake_module_2');
+ test.equal(fake_module_2.bar(), 'real bar');
+
+ mock_console.verify();
+ mock_console.restore();
+ test.done();
+ }
+ })
+ })
+ }),
+
"when an allowable is registered for unhooking": testCase({
setUp: function (callback) {
mockery.registerAllowable('./fixtures/fake_module', true);

0 comments on commit 126024e

Please sign in to comment.
Something went wrong with that request. Please try again.