Comparing version 0.1.8 to 0.1.9
@@ -60,11 +60,11 @@ /* | ||
function never(err) { | ||
assert.equal(timesCalled, 0, err); | ||
times(0, err); | ||
} | ||
function calledOnce(err) { | ||
assert.equal(timesCalled, 1, err); | ||
times(1, err); | ||
} | ||
function calledTwice(err) { | ||
assert.equal(timesCalled, 2, err); | ||
times(2, err); | ||
} | ||
@@ -217,3 +217,25 @@ | ||
function stub(methods) { | ||
function func() { | ||
var objHarness = { | ||
value: function() {} | ||
}; | ||
var wrapped = wrap(objHarness); | ||
function createFunc() { | ||
var returnVal = wrapped.value.apply(objHarness, arguments); | ||
return returnVal; | ||
} | ||
createFunc.expect = wrapped.expect.value; | ||
createFunc.setup = wrapped.setup.value; | ||
return Object.freeze(createFunc); | ||
} | ||
function stub(target) { | ||
var methods = []; | ||
if (!utils.hasValue(target.length)) { | ||
methods = utils.methods(target); | ||
} else { | ||
methods = target; | ||
} | ||
var stubObj = {}; | ||
@@ -231,3 +253,4 @@ var emptyMethod = function() { | ||
wrap: wrap, | ||
stub: stub | ||
stub: stub, | ||
func: func | ||
}; |
@@ -16,2 +16,6 @@ 'use strict'; | ||
function hasValue(obj){ | ||
return obj !== null && obj !== undefined; | ||
} | ||
function methods(obj) { | ||
@@ -39,3 +43,4 @@ var funcs = []; | ||
proxyFunctions: proxyFunctions, | ||
methods: methods | ||
methods: methods, | ||
hasValue : hasValue | ||
}; |
{ | ||
"name": "deride", | ||
"description": "Mocking library based on composition", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"homepage": "https://github.com/REAANDREW/deride", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -1,6 +0,6 @@ | ||
# deride [![Build Status](https://travis-ci.org/REAANDREW/deride.svg?branch=master)](https://travis-ci.org/REAANDREW/deride) [![NPM version](https://badge.fury.io/js/deride.svg)](http://badge.fury.io/js/deride) ![Dependencies Status](https://david-dm.org/reaandrew/deride.png) | ||
# deride [![Build Status](https://travis-ci.org/REAANDREW/deride.svg?branch=master)](https://travis-ci.org/REAANDREW/deride) [![NPM version](https://badge.fury.io/js/deride.svg)](http://badge.fury.io/js/deride) [![Dependency Status](https://david-dm.org/REAANDREW/deride.svg)](https://david-dm.org/REAANDREW/deride) | ||
Mocking library based on composition | ||
The inspiration for this was that my colleague was having a look at other mocking frameworks and mentioned to me that they do not work when using ```Object.freeze``` in the objects to enforce encapsulation. This library builds on composition to create a mocking library that can work with objects which are frozen. **It is really for the purpose of example since I am looking into using ```Object.freeze``` heavily in my current and future JavaScript develoment.** It is also developed with a very strict jshint profile, which is sometimes a challenge in itself but never the less a rewarding one. | ||
The inspiration for this was that my colleague was having a look at other mocking frameworks and mentioned to me that they do not work when using ```Object.freeze``` in the objects to enforce encapsulation. This library builds on composition to create a mocking library that can work with objects which are frozen. | ||
@@ -19,3 +19,10 @@ ## Getting Started | ||
- deride.wrap(obj) | ||
**CAUTION** Remember when you use this function about the good practice recommended in the book **Growing Object-Oriented Software, Guided by Tests** ***Chapter 8: Only Mock Types That You Own*** | ||
- deride.stub(methods) | ||
- **methods** Array | ||
- deride.stub(obj) | ||
- **obj** Object | ||
- deride.func() | ||
@@ -243,3 +250,22 @@ ### Expectations | ||
### Creating a stubbed object based on an existing object | ||
```javascript | ||
var Person = { | ||
greet: function(name) { | ||
return 'alice sas hello to ' + name; | ||
}, | ||
}; | ||
var bob = deride.stub(Person); | ||
bob.greet('alice'); | ||
bob.expect.greet.called.once(); | ||
``` | ||
### Creating a single mocked method | ||
```javascript | ||
var func = deride.func(); | ||
func.setup.toReturn(1); | ||
var value = func(1, 2, 3); | ||
assert.equal(value, 1); | ||
``` | ||
## Contributing | ||
@@ -262,6 +288,12 @@ Please ensure that you run ```grunt```, have no style warnings and that all the tests are passing. | ||
- v0.1.6 - 25th April 2014 | ||
- v0.1.8 - 28th April 2014 | ||
- Pull request from stono to support custom error messages | ||
- Made the withArgs evaluate equivalence of the args not strict object equality | ||
- v0.1.9 - 29th April 2014 | ||
- Pull request from stono to eliminate some duplication | ||
- Pull request from jamlen to fix the dependencies badge | ||
- Enable stubbing an existing object to generate the method names which exist on the target object | ||
- Enable creating a single mocked method with all the setup and expectation capabilities | ||
## License | ||
@@ -268,0 +300,0 @@ Copyright (c) 2014 Andrew Rea |
@@ -36,3 +36,2 @@ /* | ||
it('does not invoke original method when override method body', function() { | ||
var obj = deride.stub(['send']); | ||
@@ -50,3 +49,23 @@ obj.setup.send.toThrow('bang'); | ||
describe('Single function', function() { | ||
it('can setup a return value', function(done) { | ||
var func = deride.func(); | ||
func.setup.toReturn(1); | ||
var value = func(1, 2, 3); | ||
assert.equal(value, 1); | ||
done(); | ||
}); | ||
it('can setup to invoke a callback', function(done) { | ||
var func = deride.func(); | ||
func.setup.toCallbackWith(['hello', 'world']); | ||
func(function(arg1, arg2) { | ||
assert.equal(arg1, 'hello'); | ||
assert.equal(arg2, 'world'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('Eventing', function() { | ||
@@ -78,2 +97,17 @@ it('should allow the force of an emit', function(done) { | ||
}, { | ||
name: 'Creating a stub object from an object with Object style methods', | ||
setup: function() { | ||
var Person = { | ||
greet: function(name) { | ||
return 'alice sas hello to ' + name; | ||
}, | ||
chuckle: function() {}, | ||
foobar: fooBarFunction | ||
}; | ||
var stub = deride.stub(Person); | ||
stub.setup.foobar.toDoThis(fooBarFunction); | ||
return stub; | ||
} | ||
}, { | ||
name: 'Wrapping existing objects with Object style methods', | ||
@@ -143,3 +177,3 @@ setup: function() { | ||
assert.throws(function() { | ||
bob.expect.greet.called.times(1, 'This is a bespoke error'); | ||
bob.expect.greet.called.times(1, 'This is a bespoke error'); | ||
}, /This is a bespoke error/); | ||
@@ -159,3 +193,3 @@ done(); | ||
assert.throws(function() { | ||
bob.expect.greet.called.once('This is a bespoke error'); | ||
bob.expect.greet.called.once('This is a bespoke error'); | ||
}, /This is a bespoke error/); | ||
@@ -176,3 +210,3 @@ done(); | ||
assert.throws(function() { | ||
bob.expect.greet.called.twice('This is a bespoke error'); | ||
bob.expect.greet.called.twice('This is a bespoke error'); | ||
}, /This is a bespoke error/); | ||
@@ -204,3 +238,3 @@ done(); | ||
}); | ||
it('enables overriding a methods body', function(done) { | ||
@@ -207,0 +241,0 @@ bob = deride.wrap(bob); |
34378
644
301