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

Implemented spy.returns method #35

Merged
merged 1 commit into from Oct 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
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
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
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
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