Permalink
Browse files

Tests for allowables and unhooking.

  • Loading branch information...
1 parent d182626 commit 8951736f816347cfda90a9cff2c4a0f271c5e295 @mfncooper committed Apr 8, 2012
Showing with 112 additions and 0 deletions.
  1. +112 −0 test/mockery.functional.js
View
@@ -5,13 +5,23 @@
var testCase = require('nodeunit').testCase;
var mockery = require('../mockery');
var sinon = require('sinon');
+var m = require('module');
var mock_fake_module = {
foo: function () {
return 'mocked foo';
}
};
+function isCached(name) {
+ // Super-simplistic, but good enough for the tests
+ for (var id in m._cache) {
+ if (id.indexOf(name) !== -1) return true;
+ }
+
+ return false;
+}
+
module.exports = testCase({
setUp: function (callback) {
callback();
@@ -82,6 +92,108 @@ module.exports = testCase({
})
}),
+ "when an allowable is registered": testCase({
+ setUp: function (callback) {
+ mockery.registerAllowable('./fixtures/fake_module');
+ callback();
+ },
+
+ "and mockery is enabled": testCase({
+ setUp: function (callback) {
+ mockery.enable();
+ callback();
+ },
+
+ "requiring the module 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');
+
+ mock_console.verify();
+ mock_console.restore();
+ test.done();
+ },
+
+ "and the allowable is deregistered": testCase({
+ setUp: function (callback) {
+ mockery.deregisterAllowable('./fixtures/fake_module');
+ callback();
+ },
+
+ "requiring the module causes a warning to be logged": function (test) {
+ var mock_console = sinon.mock(console);
+ mock_console.expects('warn').once();
+
+ var fake_module = require('./fixtures/fake_module');
+ test.equal(fake_module.foo(), 'real foo');
+
+ 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);
+ callback();
+ },
+
+ "and mockery is enabled": testCase({
+ setUp: function (callback) {
+ if (!this.originalCache) {
+ // Initialise a clean cache
+ this.originalCache = m._cache;
+ m._cache = {};
+ }
+ mockery.enable();
+ callback();
+ },
+
+ tearDown: function (callback) {
+ if (this.originalCache) {
+ // Restore the original cache
+ m._cache = this.originalCache;
+ this.originalCache = null;
+ }
+ callback();
+ },
+
+ "the module is not cached": function (test) {
+ test.ok(!isCached('fixtures/fake_module'));
+ test.done();
+ },
+
+ "and the module is required": testCase({
+ setUp: function (callback) {
+ var fake_module = require('./fixtures/fake_module');
+ callback();
+ },
+
+ "the module is cached": function (test) {
+ test.ok(isCached('fixtures/fake_module'));
+ test.done();
+ },
+
+ "and the module is deregistered": testCase({
+ setUp: function (callback) {
+ mockery.deregisterAllowable('./fixtures/fake_module');
+ callback();
+ },
+
+ "the module is not cached": function (test) {
+ test.ok(!isCached('fixtures/fake_module'));
+ test.done();
+ }
+ })
+ }),
+ })
+ }),
+
"when a mock is registered": testCase({
setUp: function (callback) {
mockery.registerMock('./fixtures/fake_module', mock_fake_module);

0 comments on commit 8951736

Please sign in to comment.