
sinon-bluebird
Obsolete: This package is obsolete as sinon
now natively supports the .resolves
and .rejects
methods
along with setting a global promise
constructor via the .usingPromise()
method.
A plugin that adds bluebird promise helper methods to Sinon.
Installation
Run npm install sinon-bluebird
or git clone
then npm install
Optionally run unit tests: npm test
Dependencies
This package requires that both sinon
and bluebird
already be installed.
This package has peerDependencies of the following:
If you need a lower version, create an issue and it can be adjusted as necessary. Please, if you do need a lower version, test it first and then make the request.
Usage
var sinon = require('sinon')
var BPromise = require('bluebird')
require('sinon-bluebird')
var obj = {
foo: function foo() {
return 'bar'
}
}
sinon.stub(obj, 'foo').resolves('hello world!')
obj.foo().then(function(val) {
})
obj.foo.restore()
sinon.stub(obj, 'foo').rejects('AHHHHHH!!!!')
obj.foo().catch(function(e) {
});
obj.foo.restore()
obj.foo()
var obj = {
returnMethod: function (val) {
return BPromise.resolve(val)
},
paramMethod: function (val, prom, val2) {
return true
}
}
var spy = sinon.spy(obj, 'returnMethod')
obj.returnMethod('Hello')
spy.returnedPromise('Hello')
obj.returnMethod('World')
spy.alwaysReturnedPromise('Hello')
spy.restore()
spy = sinon.spy(obj, 'paramMethod')
obj.paramMethod(BPromise.resolve('Hello'))
spy.calledWithPromise('Hello')
obj.paramMethod.reset()
obj.paramMethod('Hello', BPromise.reject('World'), '!')
spy.calledWithMatch('Hello', 'World', String)
obj.paramMethod.restore()
API
Stubs
.resolves(value)
Returns a resolved bluebird promise with the given value
.rejects(value)
Returns a rejected bluebird promise with the given value.
Note: If the given value is a String, that string will be wrapped in an Error object and will be on the message property. (Removed in >=2.0.0)
Spies
All spy methods are identical to sinons version except it automatically unwraps the bluebird promise (if a promise is returned) and compares directly to the unwrapped value. We append the word Promise
to each method to denote it unwraps a promise.
Return Value Methods
Promises cannot be in a pending state otherwise an error will be thrown.
.returnedPromise(value)
.alwaysReturnedPromise(value)
Called With Methods
The promise (or promises) can be anywhere in the list of arguments passed into the spied on function or none at all.
Promises cannot be in a pending state otherwise an error will be thrown.
.calledWithPromise(value, ...)
.calledWithMatchPromise(value, ...)
.alwaysCalledWithPromise(value, ...)
.alwaysCalledWithMatchPromise(value, ...)
.calledWithExactlyPromise(value, ...)
.alwaysCalledWithExactlyPromise(value, ...)
Inspiration
Thanks to sinon-as-promised for inspiration.
If you're wondering what the main differences are:
sinon-as-promised
allows other promise libraries to be used instead of bluebird (sinon-bluebird
is designed and optimized for use only with bluebird)
sinon-as-promised
only supports .then
, .catch
, and .finally
methods off of the stub (no special bluebird methods like: .map
, .bind
, .spread
, etc...)
Contributors
License
MIT