Permalink
Please sign in to comment.
Browse files
updated interface for version 0.5
Plus complete re-write of the tests using Mocha and expect.js
- Loading branch information...
Showing
with
235 additions
and 534 deletions.
- +1 −0 .gitignore
- +7 −4 Makefile
- +61 −116 README.md
- +0 −50 example/node-qunit.js
- +126 −76 lib/pretendr.js
- +40 −38 package.json
- +0 −19 test/browser-tesing.html
- +0 −33 test/loader.js
- +0 −7 test/runner.js
- +0 −6 test/setup.js
- +0 −185 test/tests.js
@@ -0,0 +1 @@ | ||
+node_modules |
@@ -1,4 +1,7 @@ | ||
-test : | ||
- node ./test/runner.js | ||
- | ||
-.PHONY: install test | ||
+test: | ||
+ ./node_modules/.bin/mocha | ||
+ | ||
+lint: | ||
+ ./node_modules/.bin/jslint ./lib/pretendr.js ./test/spec.js | ||
+ | ||
+.PHONY: test |
@@ -1,116 +1,61 @@ | ||
-#pretendr | ||
-_A simple JavaScript mocking function_ | ||
- | ||
-pretendr (_formerly Mockery_) will mock your objects for you. It can be used in any JavaScript | ||
-testing framework. It's easy. | ||
- | ||
-##Usage | ||
- | ||
-````javascript | ||
-var myObj = { | ||
- aFunction : function () { | ||
- // do some stuff | ||
- } | ||
-}; | ||
-```` | ||
- | ||
-Then in the browser: | ||
- | ||
-````javascript | ||
-var myMock = PRETENDR(myObj); | ||
-```` | ||
- | ||
-Or in node: | ||
- | ||
-````javascript | ||
-var pretendr = require('pretendr'); | ||
-var myMock = pretendr(myObj); | ||
-```` | ||
- | ||
-Now `myMock.aFunction()` doesn't do stuff. | ||
- | ||
-pretendr works by creating a _deep copy_ of your object, swapping the | ||
-functions for new ones which are embellished with helpful mocking features. | ||
- | ||
-###Return values | ||
-To set a return value, use `myMock.aFunction.setReturnValue('a string');`. Now | ||
-calling `myMock.aFunction()` returns `'a string'`. You can also pass multiple | ||
-values, and the function will loop through them: | ||
- | ||
-````javascript | ||
-myMock.aFunction.setReturnValue('one', 'two', 'three'); | ||
- | ||
-myMock.aFunction(); // 'one' | ||
-myMock.aFunction(); // 'two' | ||
-myMock.aFunction(); // 'three' | ||
-myMock.aFunction(); // 'one' | ||
-```` | ||
- | ||
-Or use a function to set the return value: | ||
- | ||
-````javascript | ||
-myMock.aFunction.setFunction(function (arg1, arg2) { | ||
- if (arg1 === 'one' && arg2 === 'two') { | ||
- return true; | ||
- } | ||
- return false; | ||
-}); | ||
-```` | ||
- | ||
-Of course you could use `setFunction` to do other stuff too, but before you do, | ||
-read the next section. | ||
- | ||
-###Call monitoring | ||
-All calls to mocked functions are recorded in the `calls` property, which is an | ||
-array of function calls. To get the number of times a function has been called | ||
-`myMock.aFunction.calls.length`. | ||
- | ||
-You can also get the argument values of each call: | ||
- | ||
-````javascript | ||
-myMock.aFunction("here's an argument", true); | ||
-myMock.aFunction.calls[0]; // => ["here's an argument", true] | ||
-```` | ||
- | ||
-As a shorthand, `calls.last` is the last element of the array (ie, the most | ||
-recent call). | ||
- | ||
-Sometimes, you might want to find out what a particular call returned (for | ||
-example, if your `setFunction` creates a new mock object). Then you can use: | ||
- | ||
-````javascript | ||
-var returnedObj = myMock.aFunction.calls[0].returned; | ||
-```` | ||
- | ||
-The `returned` property is stored for every function call. | ||
- | ||
-###Recursion | ||
-pretendr recursively mocks your objects. So if your object contains more | ||
-objects, they too will be mocked. If it contains an array, a new array will | ||
-be created and it's elements will all be mocked. Primitive values are copied | ||
-from your object to the mock object, and you can change these at will. | ||
- | ||
-##Installation | ||
- | ||
-###browser | ||
-Download | ||
-[pretendr.js](http://github.com/nathanmacinnes/pretendr/blob/master/source/pretendr.js) | ||
-and include it in your test suite's HTML file. | ||
- | ||
-###node.js | ||
-To install via NPM, `npm install pretendr`. Then include it in your test files: | ||
-`var pretendr = require('pretendr.js');`. | ||
- | ||
-Or `git clone git://github.com/nathanmacinnes/pretendr.git`. Then | ||
-`var pretendr = require('path/to/pretendr/source/pretendr.js');`. | ||
- | ||
-##Known issues/To-do | ||
- | ||
-* Circular references will cause infinite recursion. Make sure there are no | ||
-circular references in your objects until this is fixed. | ||
-* A class mocking interface would be nice. There're a few issues to work out | ||
-before that can happen though. | ||
-* I've just implemented including properties in the prototype chain in the | ||
-mocks. You can do this by passing `true` as the second argument to `pretendr`. | ||
-There is huge potential for bugs in this though, because the implementation is | ||
-crude, so it needs to be made more robust. | ||
+# pretendr # | ||
+ | ||
+_Powerful JavaScript mocking_ | ||
+ | ||
+## Install it ## | ||
+ | ||
+`npm install pretendr`. | ||
+ | ||
+## Use it ## | ||
+ | ||
+First include it in the usual way. | ||
+ | ||
+````javascript | ||
+var pretendr = require('pretendr'); | ||
+```` | ||
+ | ||
+Then mock your objects. You can pass in real objects but I prefer to create a dummy | ||
+with only the bits I need. | ||
+ | ||
+````javascript | ||
+var mockFs = pretendr({ | ||
+ readFile : function () {}, | ||
+ readFileSync : function () {} | ||
+}); | ||
+```` | ||
+ | ||
+`mockFs` now contains a `mock` property, which is what you pass in to your code for | ||
+testing as a substitute for the real thing. | ||
+ | ||
+````javascript | ||
+var fs = mockFs.mock; | ||
+fs.readFile('f.txt', cb); | ||
+fs.readFileSync('f.txt'); | ||
+```` | ||
+ | ||
+It also has a list of properties to help you with your testing. First, record | ||
+the calls. These are the most useful, but there are plenty of others. | ||
+ | ||
+````javascript | ||
+assert.equal(fs.readFile.calls[0].args[1], 'f.txt'); | ||
+assert.equal(fs.readFile.calls[0].context, fs.mock); | ||
+ | ||
+// finish reading and pass in some data | ||
+mockFs.readFile.calls[0].callback(null, 'dummy data'); | ||
+// or set the return value of the sync version before it's called | ||
+mockFs.readFileSync.returnValue('dummy data'); | ||
+// or set a dummy function to run when it is called | ||
+mockFs.readFileSync.fake(function (filename) { | ||
+ var result; | ||
+ ... | ||
+ return result; | ||
+}); | ||
+```` | ||
+ | ||
+There are plenty of other features. To find out about them, have a look at the | ||
+spec in the `test` directory. | ||
+ | ||
+## Share it ## | ||
+ | ||
+**pretendr** is under the [MIT License](http://www.opensource.org/licenses/MIT). | ||
+[Fork it](https://github.com/nathanmacinnes/pretendr). Modify it. Pass it around. |
@@ -1,50 +0,0 @@ | ||
-/* | ||
- * 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"); | ||
-}); |

Oops, something went wrong.
0 comments on commit
139e9ce