testdouble
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -43,3 +43,3 @@ // Generated by CoffeeScript 1.10.0 | ||
calls.log(testDouble, args, this); | ||
return stubbings.get(testDouble, args); | ||
return stubbings.invoke(testDouble, args); | ||
}; | ||
@@ -46,0 +46,0 @@ }; |
@@ -35,3 +35,9 @@ // Generated by CoffeeScript 1.10.0 | ||
wasInvoked: function(testDouble, args, config) { | ||
return this.where(testDouble, args, config).length > 0; | ||
var matchingInvocationCount; | ||
matchingInvocationCount = this.where(testDouble, args, config).length; | ||
if (config.times != null) { | ||
return matchingInvocationCount === config.times; | ||
} else { | ||
return matchingInvocationCount > 0; | ||
} | ||
}, | ||
@@ -38,0 +44,0 @@ where: function(testDouble, args, config) { |
// Generated by CoffeeScript 1.10.0 | ||
(function() { | ||
var _, argsMatch, callsStore, store, stubbingFor; | ||
var _, argsMatch, callsStore, hasTimesRemaining, isSatisfied, store, stubbedValueFor, stubbingFor; | ||
@@ -16,2 +16,3 @@ _ = require('lodash'); | ||
return store["for"](testDouble).stubbings.push({ | ||
callCount: 0, | ||
stubbedValues: stubbedValues, | ||
@@ -22,13 +23,10 @@ args: args, | ||
}, | ||
get: function(testDouble, args) { | ||
var callIndex, stubbing; | ||
invoke: function(testDouble, args) { | ||
var stubbing; | ||
if (!(stubbing = stubbingFor(testDouble, args))) { | ||
return; | ||
} | ||
callIndex = callsStore.where(testDouble, args).length - 1; | ||
if (callIndex < stubbing.stubbedValues.length) { | ||
return stubbing.stubbedValues[callIndex]; | ||
} else { | ||
return _.last(stubbing.stubbedValues); | ||
} | ||
return _.tap(stubbedValueFor(stubbing), function() { | ||
return stubbing.callCount += 1; | ||
}); | ||
}, | ||
@@ -42,6 +40,25 @@ "for": function(testDouble) { | ||
return _(store["for"](testDouble).stubbings).findLast(function(stubbing) { | ||
return argsMatch(stubbing.args, actualArgs, stubbing.config); | ||
return isSatisfied(stubbing, actualArgs); | ||
}); | ||
}; | ||
stubbedValueFor = function(stubbing) { | ||
if (stubbing.callCount < stubbing.stubbedValues.length) { | ||
return stubbing.stubbedValues[stubbing.callCount]; | ||
} else { | ||
return _.last(stubbing.stubbedValues); | ||
} | ||
}; | ||
isSatisfied = function(stubbing, actualArgs) { | ||
return argsMatch(stubbing.args, actualArgs, stubbing.config) && hasTimesRemaining(stubbing); | ||
}; | ||
hasTimesRemaining = function(stubbing) { | ||
if (stubbing.config.times == null) { | ||
return true; | ||
} | ||
return stubbing.callCount < stubbing.config.times; | ||
}; | ||
}).call(this); |
@@ -162,14 +162,117 @@ // Generated by CoffeeScript 1.10.0 | ||
}); | ||
return describe('ignoring extra arguments (more thoroughly tested via when())', function() { | ||
When(function() { | ||
return this.testDouble('matters', 'not'); | ||
return describe('configuration', function() { | ||
describe('ignoring extra arguments (more thoroughly tested via when())', function() { | ||
When(function() { | ||
return this.testDouble('matters', 'not'); | ||
}); | ||
return Then(function() { | ||
return shouldNotThrow((function(_this) { | ||
return function() { | ||
return _this.verify(_this.testDouble('matters'), { | ||
ignoreExtraArgs: true | ||
}); | ||
}; | ||
})(this)); | ||
}); | ||
}); | ||
return Then(function() { | ||
return shouldNotThrow((function(_this) { | ||
return function() { | ||
return _this.verify(_this.testDouble('matters'), { | ||
ignoreExtraArgs: true | ||
}); | ||
}; | ||
})(this)); | ||
return describe('number of times an invocation is satisfied', function() { | ||
context('0 times, satisfied', function() { | ||
return Then(function() { | ||
return shouldNotThrow((function(_this) { | ||
return function() { | ||
return _this.verify(_this.testDouble(), { | ||
times: 0 | ||
}); | ||
}; | ||
})(this)); | ||
}); | ||
}); | ||
context('0 times, unsatisfied', function() { | ||
When(function() { | ||
return this.testDouble(); | ||
}); | ||
return Then(function() { | ||
return shouldThrow(((function(_this) { | ||
return function() { | ||
return _this.verify(_this.testDouble(), { | ||
times: 0 | ||
}); | ||
}; | ||
})(this)), "Unsatisfied verification on test double.\n\n Wanted:\n - called with `()` 0 times.\n\n But was actually called:\n - called with `()`."); | ||
}); | ||
}); | ||
context('1 time, satisfied', function() { | ||
When(function() { | ||
return this.testDouble(); | ||
}); | ||
return Then(function() { | ||
return shouldNotThrow((function(_this) { | ||
return function() { | ||
return _this.verify(_this.testDouble(), { | ||
times: 1 | ||
}); | ||
}; | ||
})(this)); | ||
}); | ||
}); | ||
context('1 time, unsatisfied (with 2)', function() { | ||
When(function() { | ||
return this.testDouble(); | ||
}); | ||
And(function() { | ||
return this.testDouble(); | ||
}); | ||
return Then(function() { | ||
return shouldThrow(((function(_this) { | ||
return function() { | ||
return _this.verify(_this.testDouble(), { | ||
times: 1 | ||
}); | ||
}; | ||
})(this)), "Unsatisfied verification on test double.\n\n Wanted:\n - called with `()` 1 time.\n\n But was actually called:\n - called with `()`.\n - called with `()`."); | ||
}); | ||
}); | ||
context('4 times, satisfied', function() { | ||
When(function() { | ||
return this.testDouble(); | ||
}); | ||
And(function() { | ||
return this.testDouble(); | ||
}); | ||
And(function() { | ||
return this.testDouble(); | ||
}); | ||
And(function() { | ||
return this.testDouble(); | ||
}); | ||
return Then(function() { | ||
return shouldNotThrow((function(_this) { | ||
return function() { | ||
return _this.verify(_this.testDouble(), { | ||
times: 4 | ||
}); | ||
}; | ||
})(this)); | ||
}); | ||
}); | ||
return context('4 times, unsatisfied (with 3)', function() { | ||
When(function() { | ||
return this.testDouble(); | ||
}); | ||
And(function() { | ||
return this.testDouble(); | ||
}); | ||
And(function() { | ||
return this.testDouble(); | ||
}); | ||
return Then(function() { | ||
return shouldThrow(((function(_this) { | ||
return function() { | ||
return _this.verify(_this.testDouble(), { | ||
times: 4 | ||
}); | ||
}; | ||
})(this)), "Unsatisfied verification on test double.\n\n Wanted:\n - called with `()` 4 times.\n\n But was actually called:\n - called with `()`.\n - called with `()`.\n - called with `()`."); | ||
}); | ||
}); | ||
}); | ||
@@ -176,0 +279,0 @@ }); |
@@ -10,6 +10,6 @@ // Generated by CoffeeScript 1.10.0 | ||
}); | ||
Given(function() { | ||
return this.testDouble = this.create(); | ||
}); | ||
describe('no-arg stubbing', function() { | ||
Given(function() { | ||
return this.testDouble = this.create(); | ||
}); | ||
context('foo', function() { | ||
@@ -34,5 +34,2 @@ Given(function() { | ||
Given(function() { | ||
return this.testDouble = this.create(); | ||
}); | ||
Given(function() { | ||
return this.when(this.testDouble("something")).thenReturn("gold"); | ||
@@ -49,5 +46,2 @@ }); | ||
Given(function() { | ||
return this.testDouble = this.create(); | ||
}); | ||
Given(function() { | ||
return this.when(this.testDouble(1)).thenReturn("foo"); | ||
@@ -117,5 +111,2 @@ }); | ||
Given(function() { | ||
return this.testDouble = this.create(); | ||
}); | ||
Given(function() { | ||
return this.when(this.testDouble(88, this.matchers.isA(Number))).thenReturn("yay"); | ||
@@ -134,63 +125,106 @@ }); | ||
describe('stubbing sequential returns', function() { | ||
Given(function() { | ||
return this.testDouble = this.create(); | ||
}); | ||
Given(function() { | ||
return this.when(this.testDouble()).thenReturn(10, 9); | ||
}); | ||
When(function() { | ||
var ref; | ||
return ref = [this.testDouble(), this.testDouble(), this.testDouble()], this.first = ref[0], this.second = ref[1], this.third = ref[2], ref; | ||
}); | ||
Then(function() { | ||
return this.first === 10; | ||
}); | ||
Then(function() { | ||
return this.second === 9; | ||
}); | ||
return Then(function() { | ||
return this.third === 9; | ||
}); | ||
}); | ||
return describe('ignoring extra arguments', function() { | ||
Given(function() { | ||
return this.testDouble = this.create(); | ||
}); | ||
context('for a no-arg stubbing', function() { | ||
context('a single stubbing', function() { | ||
Given(function() { | ||
return this.when(this.testDouble(), { | ||
ignoreExtraArgs: true | ||
}).thenReturn('pewpew'); | ||
return this.when(this.testDouble()).thenReturn(10, 9); | ||
}); | ||
When(function() { | ||
return this.result = this.testDouble('so', 'many', 'args'); | ||
var ref; | ||
return ref = [this.testDouble(), this.testDouble(), this.testDouble()], this.first = ref[0], this.second = ref[1], this.third = ref[2], ref; | ||
}); | ||
Then(function() { | ||
return this.first === 10; | ||
}); | ||
Then(function() { | ||
return this.second === 9; | ||
}); | ||
return Then(function() { | ||
return this.result === 'pewpew'; | ||
return this.third === 9; | ||
}); | ||
}); | ||
return context('when an initial-arg-matters', function() { | ||
return context('two overlapping stubbings', function() { | ||
Given(function() { | ||
return this.when(this.testDouble('important'), { | ||
ignoreExtraArgs: true | ||
}).thenReturn('neat'); | ||
return this.when(this.testDouble()).thenReturn('A'); | ||
}); | ||
context('satisfied without extra args', function() { | ||
Given(function() { | ||
return this.testDouble(); | ||
}); | ||
Given(function() { | ||
return this.when(this.testDouble()).thenReturn('B', 'C'); | ||
}); | ||
return Then(function() { | ||
return this.testDouble() === 'B'; | ||
}); | ||
}); | ||
}); | ||
return describe('config object', function() { | ||
describe('ignoring extra arguments', function() { | ||
context('for a no-arg stubbing', function() { | ||
Given(function() { | ||
return this.when(this.testDouble(), { | ||
ignoreExtraArgs: true | ||
}).thenReturn('pewpew'); | ||
}); | ||
When(function() { | ||
return this.result = this.testDouble('so', 'many', 'args'); | ||
}); | ||
return Then(function() { | ||
return this.testDouble('important') === 'neat'; | ||
return this.result === 'pewpew'; | ||
}); | ||
}); | ||
context('satisfied with extra args', function() { | ||
return Then(function() { | ||
return this.testDouble('important', 'not important') === 'neat'; | ||
return context('when an initial-arg-matters', function() { | ||
Given(function() { | ||
return this.when(this.testDouble('important'), { | ||
ignoreExtraArgs: true | ||
}).thenReturn('neat'); | ||
}); | ||
context('satisfied without extra args', function() { | ||
return Then(function() { | ||
return this.testDouble('important') === 'neat'; | ||
}); | ||
}); | ||
context('satisfied with extra args', function() { | ||
return Then(function() { | ||
return this.testDouble('important', 'not important') === 'neat'; | ||
}); | ||
}); | ||
context('unsatisfied with no args', function() { | ||
return Then(function() { | ||
return this.testDouble() === void 0; | ||
}); | ||
}); | ||
return context('unsatisfied with extra args', function() { | ||
return Then(function() { | ||
return this.testDouble('unimportant', 'not important') === void 0; | ||
}); | ||
}); | ||
}); | ||
context('unsatisfied with no args', function() { | ||
}); | ||
return describe('limiting times stubbing will work', function() { | ||
context('a single stub', function() { | ||
Given(function() { | ||
return this.when(this.testDouble(), { | ||
times: 2 | ||
}).thenReturn('pants'); | ||
}); | ||
When(function() { | ||
return this.result = [this.testDouble(), this.testDouble(), this.testDouble()]; | ||
}); | ||
return Then(function() { | ||
return this.testDouble() === void 0; | ||
return expect(this.result).to.deep.equal(['pants', 'pants', void 0]); | ||
}); | ||
}); | ||
return context('unsatisfied with extra args', function() { | ||
return context('two overlapping stubbings', function() { | ||
Given(function() { | ||
return this.when(this.testDouble()).thenReturn('NO'); | ||
}); | ||
Given(function() { | ||
return this.when(this.testDouble(), { | ||
times: 1 | ||
}).thenReturn('YES'); | ||
}); | ||
When(function() { | ||
return this.result = [this.testDouble(), this.testDouble(), this.testDouble()]; | ||
}); | ||
return Then(function() { | ||
return this.testDouble('unimportant', 'not important') === void 0; | ||
return expect(this.result).to.deep.equal(['YES', 'NO', 'NO']); | ||
}); | ||
@@ -197,0 +231,0 @@ }); |
// Generated by CoffeeScript 1.10.0 | ||
(function() { | ||
var _, callsStore, invocationSummary, store, stringifyArgs, stringifyName, unsatisfiedErrorMessage; | ||
var _, callsStore, invocationSummary, store, stringifyArgs, stringifyName, timesMessage, unsatisfiedErrorMessage; | ||
@@ -22,3 +22,3 @@ _ = require('lodash'); | ||
} else { | ||
throw new Error(unsatisfiedErrorMessage(last.testDouble, last.args)); | ||
throw new Error(unsatisfiedErrorMessage(last.testDouble, last.args, config)); | ||
} | ||
@@ -30,4 +30,4 @@ } else { | ||
unsatisfiedErrorMessage = function(testDouble, args) { | ||
return ("Unsatisfied verification on test double" + (stringifyName(testDouble)) + ".\n\n Wanted:\n - called with `(" + (stringifyArgs(args)) + ")`.") + invocationSummary(testDouble); | ||
unsatisfiedErrorMessage = function(testDouble, args, config) { | ||
return ("Unsatisfied verification on test double" + (stringifyName(testDouble)) + ".\n\n Wanted:\n - called with `(" + (stringifyArgs(args)) + ")`" + (timesMessage(config)) + ".") + invocationSummary(testDouble); | ||
}; | ||
@@ -56,2 +56,9 @@ | ||
timesMessage = function(config) { | ||
if (config.times == null) { | ||
return ""; | ||
} | ||
return " " + config.times + " time" + (config.times === 1 ? '' : 's'); | ||
}; | ||
}).call(this); |
{ | ||
"name": "testdouble", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "A minimal test double library for TDD with JavaScript", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/testdouble/testdouble.js", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
512673
13537