Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Functional tests from Bryan Donovan.

  • Loading branch information...
commit fb22ecc32c43ebf9711141b79a42b7dbbfeb6611 1 parent e03e317
Martin Cooper authored
14 package.json
View
@@ -1,5 +1,11 @@
{
"author": "Martin Cooper <mfncooper@gmail.com>",
+ "contributors": [
+ {
+ "name": "Bryan Donovan",
+ "email": "bdondo@gmail.com"
+ }
+ ],
"name": "mockery",
"description": "Simplifying the use of mocks with Node.js",
"version": "1.1.1",
@@ -15,7 +21,13 @@
"node": ">=0.4.5"
},
"dependencies": {},
- "devDependencies": {},
+ "devDependencies": {
+ "nodeunit" : "0.6.x",
+ "sinon" : "1.2.x"
+ },
+ "scripts": {
+ "test": "test/run.js"
+ },
"licenses" : [
{
"type" : "MIT",
5 test/fixtures/fake_module.js
View
@@ -0,0 +1,5 @@
+var foo = function() {
+ return 'real foo';
+};
+
+exports.foo = foo;
5 test/fixtures/substitute_fake_module.js
View
@@ -0,0 +1,5 @@
+var foo = function() {
+ return 'substitute foo';
+};
+
+exports.foo = foo;
107 test/mockery.functional.js
View
@@ -0,0 +1,107 @@
+/*
+ * Run with nodeunit:
+ * nodeunit --reporter nested mockery.functional.js
+ */
+var testCase = require('nodeunit').testCase;
+var mockery = require('../mockery');
+var sinon = require('sinon');
+
+var mock_fake_module = {
+ foo: function () {
+ return 'mocked foo';
+ }
+};
+
+module.exports = testCase({
+ setUp: function (callback) {
+ callback();
+ },
+
+ tearDown: function (callback) {
+ mockery.disable();
+ mockery.deregisterAll();
+ callback();
+ },
+
+ "when a mock is not registered": testCase({
+ "and mockery is enabled": testCase({
+ setUp: function (callback) {
+ mockery.enable();
+ callback();
+ },
+
+ "requiring a 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 a mock is registered": testCase({
+ setUp: function (callback) {
+ mockery.registerMock('./fixtures/fake_module', mock_fake_module);
+ callback();
+ },
+
+ "and mockery is enabled": testCase({
+ setUp: function (callback) {
+ mockery.enable();
+ callback();
+ },
+
+ "requiring the module returns the mock instead": function (test) {
+ var fake_module = require('./fixtures/fake_module');
+ test.equal(fake_module.foo(), 'mocked foo');
+ test.done();
+ },
+
+ "and the mock is deregistered": testCase({
+ "requiring the module returns the original module": function (test) {
+ mockery.deregisterMock('./fixtures/fake_module', mock_fake_module);
+ mockery.registerAllowable('./fixtures/fake_module');
+ var fake_module = require('./fixtures/fake_module');
+ test.equal(fake_module.foo(), 'real foo');
+ test.done();
+ }
+ }),
+
+ "and mockery is then disabled": testCase({
+ "requiring the module returns the original module": function (test) {
+ mockery.disable();
+ var fake_module = require('./fixtures/fake_module');
+ test.equal(fake_module.foo(), 'real foo');
+ test.done();
+ }
+ })
+ })
+ }),
+
+ "when a substitute is registered": testCase({
+ setUp: function (callback) {
+ mockery.registerSubstitute('./fixtures/fake_module',
+ './fixtures/substitute_fake_module');
+ callback();
+ },
+
+ "and mockery is enabled": testCase({
+ setUp: function (callback) {
+ mockery.enable();
+ callback();
+ },
+
+ "requiring the module returns the substitute instead": function (test) {
+ var fake_module = require('./fixtures/fake_module');
+ test.equal(fake_module.foo(), 'substitute foo');
+ test.done();
+ }
+ })
+ })
+
+});
3  test/run.js
View
@@ -0,0 +1,3 @@
+#!/usr/bin/env node
+var test_reporter = require('nodeunit').reporters.nested;
+test_reporter.run(['./test/mockery.functional.js']);
Please sign in to comment.
Something went wrong with that request. Please try again.