Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added spy.returnsInOrder method #44

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/spy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
9 changes: 9 additions & 0 deletions test/spies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down