diff --git a/lib/spy.js b/lib/spy.js index 87a331b..0ace81d 100644 --- a/lib/spy.js +++ b/lib/spy.js @@ -164,6 +164,27 @@ module.exports = function (chai, _) { }); }; + /** + * # chai.spy.returnsInOrder (function) + * + * Creates a spy which returns different value each time it is called (in defined order). + * + * var method = chai.spy.returnsInOrder(['first', 'second']); + * method().should.equal('first'); + * method().should.equal('second'); + * + * @param {Object[]} [values] array of values be returned by the spy + * @returns new spy function which returns values in given order + * @api public + */ + + chai.spy.returnsInOrder = function (values) { + var currentIndex = 0; + return chai.spy(function () { + return values[currentIndex++]; + }); + }; + /** * # spy * diff --git a/test/spies.js b/test/spies.js index bb6bd14..19ea357 100644 --- a/test/spies.js +++ b/test/spies.js @@ -224,6 +224,15 @@ describe('Chai Spies', function () { spy().should.equal(value); }); + it('should create spy which returns multiple different values in order', function(){ + var values = ['value1', 'value2'] + var spy = chai.spy.returnsInOrder(values); + + spy.should.be.a.spy; + spy().should.equal(values[0]); + spy().should.equal(values[1]); + }); + it('should spy multiple object methods passed as array', function () { var array = chai.spy.on([], 'push', 'pop');