Skip to content

Commit

Permalink
updated interface for version 0.5
Browse files Browse the repository at this point in the history
Plus complete re-write of the tests using Mocha and expect.js
  • Loading branch information
nathanmacinnes committed Jul 28, 2012
1 parent f197dd6 commit 139e9ce
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 534 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
11 changes: 7 additions & 4 deletions Makefile
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,7 @@
test : test:
node ./test/runner.js ./node_modules/.bin/mocha


.PHONY: install test lint:
./node_modules/.bin/jslint ./lib/pretendr.js ./test/spec.js

.PHONY: test
177 changes: 61 additions & 116 deletions README.md
Original file line number Original file line Diff line number Diff line change
@@ -1,116 +1,61 @@
#pretendr # pretendr #
_A simple JavaScript mocking function_

_Powerful JavaScript mocking_
pretendr (_formerly Mockery_) will mock your objects for you. It can be used in any JavaScript
testing framework. It's easy. ## Install it ##


##Usage `npm install pretendr`.


````javascript ## Use it ##
var myObj = {
aFunction : function () { First include it in the usual way.
// do some stuff
} ````javascript
}; var pretendr = require('pretendr');
```` ````


Then in the browser: 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 myMock = PRETENDR(myObj); ````javascript
```` var mockFs = pretendr({

readFile : function () {},
Or in node: readFileSync : function () {}

});
````javascript ````
var pretendr = require('pretendr');
var myMock = pretendr(myObj); `mockFs` now contains a `mock` property, which is what you pass in to your code for
```` testing as a substitute for the real thing.


Now `myMock.aFunction()` doesn't do stuff. ````javascript

var fs = mockFs.mock;
pretendr works by creating a _deep copy_ of your object, swapping the fs.readFile('f.txt', cb);
functions for new ones which are embellished with helpful mocking features. fs.readFileSync('f.txt');

````
###Return values
To set a return value, use `myMock.aFunction.setReturnValue('a string');`. Now It also has a list of properties to help you with your testing. First, record
calling `myMock.aFunction()` returns `'a string'`. You can also pass multiple the calls. These are the most useful, but there are plenty of others.
values, and the function will loop through them:

````javascript
````javascript assert.equal(fs.readFile.calls[0].args[1], 'f.txt');
myMock.aFunction.setReturnValue('one', 'two', 'three'); assert.equal(fs.readFile.calls[0].context, fs.mock);


myMock.aFunction(); // 'one' // finish reading and pass in some data
myMock.aFunction(); // 'two' mockFs.readFile.calls[0].callback(null, 'dummy data');
myMock.aFunction(); // 'three' // or set the return value of the sync version before it's called
myMock.aFunction(); // 'one' mockFs.readFileSync.returnValue('dummy data');
```` // or set a dummy function to run when it is called

mockFs.readFileSync.fake(function (filename) {
Or use a function to set the return value: var result;

...
````javascript return result;
myMock.aFunction.setFunction(function (arg1, arg2) { });
if (arg1 === 'one' && arg2 === 'two') { ````
return true;
} There are plenty of other features. To find out about them, have a look at the
return false; spec in the `test` directory.
});
```` ## Share it ##


Of course you could use `setFunction` to do other stuff too, but before you do, **pretendr** is under the [MIT License](http://www.opensource.org/licenses/MIT).
read the next section. [Fork it](https://github.com/nathanmacinnes/pretendr). Modify it. Pass it around.

###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.
50 changes: 0 additions & 50 deletions example/node-qunit.js

This file was deleted.

Loading

0 comments on commit 139e9ce

Please sign in to comment.