diff --git a/README.md b/README.md index 388c747..91ecd78 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,11 @@ array.push.reset(); //or you can create spy object var object = chai.spy.object([ 'push', 'pop' ]); object.push(5); + +// or you create spy which returns static value +var spy = chai.spy.returns(true); + +spy(); // true ``` ### Assertions diff --git a/chai-spies.js b/chai-spies.js index d4eeeda..6ff2bf9 100644 --- a/chai-spies.js +++ b/chai-spies.js @@ -158,6 +158,24 @@ }, {}); }; + /** + * # chai.spy.returns (function) + * + * Creates a spy which returns static value. + * + * var method = chai.spy.returns(true); + * + * @param {*} value static value which is returned by spy + * @returns new spy function which returns static value + * @api public + */ + + chai.spy.returns = function (value) { + return chai.spy(function () { + return value; + }); + }; + /** * # spy * diff --git a/lib/spy.js b/lib/spy.js index 991b463..c883e69 100644 --- a/lib/spy.js +++ b/lib/spy.js @@ -142,6 +142,24 @@ module.exports = function (chai, _) { }, {}); }; + /** + * # chai.spy.returns (function) + * + * Creates a spy which returns static value. + * + * var method = chai.spy.returns(true); + * + * @param {*} value static value which is returned by spy + * @returns new spy function which returns static value + * @api public + */ + + chai.spy.returns = function (value) { + return chai.spy(function () { + return value; + }); + }; + /** * # spy * diff --git a/test/spies.js b/test/spies.js index 9f1a3e1..a8edf8b 100644 --- a/test/spies.js +++ b/test/spies.js @@ -218,6 +218,14 @@ describe('Chai Spies', function () { array.should.have.length(2); }); + it('should create spy which returns static value', function() { + var value = {}; + var spy = chai.spy.returns(value); + + spy.should.be.a.spy; + spy().should.equal(value); + }); + describe('.with', function () { it('should not interfere chai with' ,function () { (1).should.be.with.a('number'); @@ -424,7 +432,7 @@ describe('Chai Spies', function () { it('should setup spy with default values when spy is instantiated', function() { var name = 'proxy'; var spy = chai.spy(name); - + spy.should.be.spy; spy.__spy.called.should.be.false; spy.__spy.calls.should.have.length(0);