Skip to content

Commit

Permalink
Merge pull request #35 from stalniy/spy.returns
Browse files Browse the repository at this point in the history
Implemented spy.returns method
  • Loading branch information
keithamus committed Oct 13, 2015
2 parents 9309677 + 0833f35 commit 58f50ec
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions chai-spies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
18 changes: 18 additions & 0 deletions lib/spy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
10 changes: 9 additions & 1 deletion test/spies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 58f50ec

Please sign in to comment.