simple-mock
Advanced tools
Comparing version 0.2.3 to 0.2.4
12
index.js
@@ -65,3 +65,2 @@ (function (global, module) { // Browser compatibility | ||
var stubFn = function() { | ||
var cb = arguments[arguments.length - 1]; | ||
var action = (newFn.loop) ? newFn.actions[(newFn.callCount - 1) % newFn.actions.length] : newFn.actions.shift(); | ||
@@ -72,2 +71,4 @@ | ||
if (action.returnValue) return action.returnValue; | ||
var cb = ('cbArgIndex' in action) ? arguments[action.cbArgIndex] : arguments[arguments.length - 1]; | ||
if (action.cbArgs) return cb.apply(null, action.cbArgs); | ||
@@ -110,2 +111,11 @@ } | ||
newFn.callbackArgWith = function() { | ||
wrappedFn = stubFn; | ||
newFn.actions.push({ | ||
cbArgs: Array.prototype.slice.call(arguments, 1), | ||
cbArgIndex: arguments[0] | ||
}); | ||
return newFn; // Chainable | ||
} | ||
newFn.returnWith = function(returnValue) { | ||
@@ -112,0 +122,0 @@ wrappedFn = stubFn; |
{ | ||
"name": "simple-mock", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "Super simple mocks, stubs, and spies with 1-step sandbox restore", | ||
@@ -15,2 +15,4 @@ "main": "index.js", | ||
"test", | ||
"sinon", | ||
"simplemock", | ||
"simple", | ||
@@ -17,0 +19,0 @@ "mock", |
@@ -102,2 +102,4 @@ # simple-mock ![Project status](https://secure.travis-ci.org/jupiter/node-simple-mock.png?branch=master) | ||
--- | ||
### spy.called | ||
@@ -123,6 +125,8 @@ | ||
### stub.callbackWith(...) | ||
--- | ||
Configures this stub to call back with the arguments passed. The callback function to be called is assumed to be the last argument passed to the stub, as per general practice, e.g. `stub(a, cb)` or `stub(a, b, cb)`. Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub. | ||
### stub.callbackWith(...) *or* stub.callbackArgWith(argumentIndex, ...) | ||
Configures this stub to call back with the arguments passed. It will use the last argument as callback, or argument at `argumentIndex`. Subsequent calls of this on the same stub (chainable) will queue up different behaviours for each subsequent call of the stub. | ||
### stub.returnWith(val) | ||
@@ -129,0 +133,0 @@ |
75
test.js
@@ -178,29 +178,60 @@ 'use strict'; | ||
describe('for a single callback configuration', function() { | ||
beforeEach(function() { | ||
stubFn = simple.stub().callbackWith(1, 2, 3); | ||
}); | ||
describe('with default index', function() { | ||
beforeEach(function() { | ||
stubFn = simple.stub().callbackWith(1, 2, 3); | ||
}); | ||
it('can call back with arguments', function() { | ||
stubFn('a', function() { | ||
assert(stubFn.called); | ||
assert(stubFn.callCount, 1); | ||
assert.equal(stubFn.lastCall.args[0], 'a'); | ||
assert.equal(arguments.length, 3); | ||
assert.equal(arguments[0], 1); | ||
assert.equal(arguments[1], 2); | ||
assert.equal(arguments[2], 3); | ||
it('can call back with arguments', function() { | ||
stubFn('a', function() { | ||
assert(stubFn.called); | ||
assert(stubFn.callCount, 1); | ||
assert.equal(stubFn.lastCall.args[0], 'a'); | ||
assert.equal(arguments.length, 3); | ||
assert.equal(arguments[0], 1); | ||
assert.equal(arguments[1], 2); | ||
assert.equal(arguments[2], 3); | ||
}); | ||
}); | ||
it('can call back with arguments, over multiple calls', function() { | ||
stubFn('a', function() {}); | ||
stubFn('b', function() { | ||
assert(stubFn.called); | ||
assert(stubFn.callCount, 2); | ||
assert.equal(stubFn.lastCall.args[0], 'b'); | ||
assert.equal(arguments.length, 3); | ||
assert.equal(arguments[0], 1); | ||
assert.equal(arguments[1], 2); | ||
assert.equal(arguments[2], 3); | ||
}); | ||
}); | ||
}); | ||
it('can call back with arguments, over multiple calls', function() { | ||
stubFn('a', function() {}); | ||
stubFn('b', function() { | ||
assert(stubFn.called); | ||
assert(stubFn.callCount, 2); | ||
assert.equal(stubFn.lastCall.args[0], 'b'); | ||
assert.equal(arguments.length, 3); | ||
assert.equal(arguments[0], 1); | ||
assert.equal(arguments[1], 2); | ||
assert.equal(arguments[2], 3); | ||
describe('with specified index', function() { | ||
beforeEach(function() { | ||
stubFn = simple.stub().callbackArgWith(1, 2, 3); | ||
}); | ||
it('can call back with arguments', function() { | ||
stubFn('a', function() { | ||
assert(stubFn.called); | ||
assert(stubFn.callCount, 1); | ||
assert.equal(stubFn.lastCall.args[0], 'a'); | ||
assert.equal(arguments.length, 2); | ||
assert.equal(arguments[0], 2); | ||
assert.equal(arguments[1], 3); | ||
}); | ||
}); | ||
it('can call back with arguments, over multiple calls', function() { | ||
stubFn('a', function() {}); | ||
stubFn('b', function() { | ||
assert(stubFn.called); | ||
assert(stubFn.callCount, 2); | ||
assert.equal(stubFn.lastCall.args[0], 'b'); | ||
assert.equal(arguments.length, 2); | ||
assert.equal(arguments[0], 2); | ||
assert.equal(arguments[1], 3); | ||
}); | ||
}); | ||
}); | ||
@@ -207,0 +238,0 @@ }); |
27286
650
153