Permalink
Browse files

Changed interface back to calling pretendr() directly

I didn't realise you could do this in node, so changed the interface for both in 0.2
so that the browser and node.js interfaces matched. I've worked out how to do it now
so changed it back, and made the version 0.3 as this is a significant interface
change.
  • Loading branch information...
1 parent e7ab3c3 commit f197dd62f2ed0309724edc2526b00f4fc051405d @nathanmacinnes committed Jan 19, 2012
Showing with 64 additions and 16 deletions.
  1. +2 −2 README.md
  2. +50 −0 example/node-qunit.js
  3. +3 −5 lib/pretendr.js
  4. +1 −1 package.json
  5. +8 −8 test/tests.js
View
@@ -17,14 +17,14 @@ var myObj = {
Then in the browser:
````javascript
-var myMock = PRETENDR.mock(myObj);
+var myMock = PRETENDR(myObj);
````
Or in node:
````javascript
var pretendr = require('pretendr');
-var myMock = pretendr.mock(myObj);
+var myMock = pretendr(myObj);
````
Now `myMock.aFunction()` doesn't do stuff.
View
@@ -0,0 +1,50 @@
+/*
+ * This shows the process for mocking a database object. The object has a
+ * single method, connect, which returns an object which has a callback
+ * called on it. This callback returns a database connection object, which
+ * we will also mock.
+ *
+ * We could mock the original object instead of creating a new one. That's
+ * just a matter of preference in this case.
+ *
+ */
+
+// pretendr has been installed via npm
+var pretendr = require('pretendr');
+
+module("Database access", {
+ setup : function () {
+ // mock the database object
+ this.database = pretendr.mock({
+ connect : function () {}
+ });
+ // set a return value, as dbUtil will want to assign a callback method to it
+ this.database.connect.setReturnValue({});
+ }
+});
+
+test("database will connect", function () {
+ dbUtil.connectToDatabase(this.database);
+
+ // check that the connection function was called once
+ equal(this.database.connect.calls.length, 1,
+ "database connection function was called");
+});
+test("database will open a transaction as soon as it has connected", function () {
+ dbUtil.connectToDatabase(this.database);
+
+ // get the return value of the function call so we can call its callback
+ // we could've saved this when we created it, but why clutter up the
+ // namespace of the other tests when we can just do this!
+ var connectionListener = this.database.connect.calls[0].returned;
+
+ // Mock the connection object which is passed to the callback
+ var databaseConnectionObject = pretendr.mock({
+ transaction : function () {}
+ });
+
+ // call the connection callback which the object should have created
+ connectionListener.onconnect(databaseConnectionObject);
+
+ equal(databaseConnectionObject.transaction.calls.length, 1, "a transaction is created");
+});
View
@@ -1,9 +1,7 @@
/*jslint maxlen: 80, indent: 4 */
/*Global exports */
-var PRETENDR = {};
-
-PRETENDR.mock = function (obj, proto) {
+var PRETENDR = function (obj, proto) {
"use strict";
var i,
mockAFunction,
@@ -64,7 +62,7 @@ PRETENDR.mock = function (obj, proto) {
// mock all the properties of objects, functions and arrays
for (i in obj) {
if (obj.hasOwnProperty(i) || proto) {
- mockObj[i] = PRETENDR.mock(obj[i]);
+ mockObj[i] = PRETENDR(obj[i]);
}
}
// make sure the user can get at the original
@@ -79,6 +77,6 @@ PRETENDR.mock = function (obj, proto) {
// need to put this in a try/catch because in the browser, export is undefined
try {
- exports.mock = PRETENDR.mock;
+ module.exports = PRETENDR;
} catch (e) {
}
View
@@ -1,7 +1,7 @@
{
"name" : "pretendr",
"description" : "A mocking function for unit testing",
- "version" : "v0.2.0",
+ "version" : "v0.3.0",
"author" : "Nathan MacInnes <nathan@macinn.es>",
"repository" : {
"type" : "git",
View
@@ -12,7 +12,7 @@
subPrimitive : 2
}
};
- this.mockObj = PRETENDR.mock(this.objectToMock);
+ this.mockObj = PRETENDR(this.objectToMock);
}
});
@@ -40,7 +40,7 @@
},
method1 : function () {}
};
- this.mockObj = PRETENDR.mock(this.objectToMock);
+ this.mockObj = PRETENDR(this.objectToMock);
}
});
@@ -57,7 +57,7 @@
fn.called = true;
};
this.objectToMock = fn;
- this.mockObj = PRETENDR.mock(this.objectToMock);
+ this.mockObj = PRETENDR(this.objectToMock);
}
});
@@ -118,7 +118,7 @@
mock;
fn.aProperty = 34;
fn.anotherProperty = {};
- mock = PRETENDR.mock(fn);
+ mock = PRETENDR(fn);
equal(mock.aProperty, fn.aProperty);
equal(mock.anotherProperty.original, fn.anotherProperty);
});
@@ -138,7 +138,7 @@
module("Mocking arrays", {
setup : function () {
this.objectToMock = [0, true, 'two'];
- this.mockObj = PRETENDR.mock(this.objectToMock);
+ this.mockObj = PRETENDR(this.objectToMock);
}
});
@@ -151,7 +151,7 @@
});
test("if arrays have non-primitive types as their elements, they will be mocked", function () {
var objectToMock = [{}, [], function () {}],
- mockObj = PRETENDR.mock(objectToMock);
+ mockObj = PRETENDR(objectToMock);
equal(mockObj[0].original, objectToMock[0]);
deepEqual(mockObj[1], objectToMock[1]);
deepEqual(mockObj[2].calls, []);
@@ -160,7 +160,7 @@
var arr = [12],
mock;
arr.aProperty = {};
- mock = PRETENDR.mock(arr);
+ mock = PRETENDR(arr);
equal(mock.aProperty.original, arr.aProperty);
});
@@ -172,7 +172,7 @@
ConstructorFn.prototype.protoFn = function () {
};
this.obj = new ConstructorFn();
- this.mock = PRETENDR.mock(this.obj, true);
+ this.mock = PRETENDR(this.obj, true);
}
});

0 comments on commit f197dd6

Please sign in to comment.