sinon-with-promise
Extend Sinon stubs and spies with promise stubbing methods. This is the extension of Sinon-as-promised.
Installing
npm install sinon-with-promise
If you're using sinon-with-promise in the browser and are not using Browserify/Webpack, use 3.x or earlier.
Usage
Stub
var sinon = sinon
Spy
var sinon = var spy = sinon spy
Using Bluebird
You'll only need to require sinon-with-promise once. It attaches the appropriate stubbing functions which will then be available anywhere else you require sinon. It defaults to using native ES6 Promise (or provides a polyfill), but you can use another promise library if you'd like, as long as it exposes a constructor:
// Using Bluebirdvar Bluebird = Bluebird
API
Stub
stub.resolves(value)
-> stub
value
Required
Type: any
When called, the stub will return a "thenable" object which will return a promise for the provided value
. Any Promises/A+ compliant library will handle this object properly.
var stub = sinon;stub; ; stub;
stub.rejects(err)
-> stub
err
Required
Type: error
/ string
When called, the stub will return a thenable which will return a reject promise with the provided err
. If err
is a string, it will be set as the message on an Error
object.
stub;stub; stub;;
Spy
spy.resolves(value)
-> spy
with promised
function
value
Required
Type: any
When called, the spy will return a "thenable" object which will return a promise for the provided value
. Any Promises/A+ compliant library will handle this object properly.
Unlike stub, spy returns the Promise
function by the promised
variable, because the spy
object should be used to do the spy work(checking calls).
var spy1 = sinon; { return }
spy.rejects(err)
-> spy
with promised
function
err
Required
Type: error
/ string
When called, the stub will return a thenable which will return a reject promise with the provided err
. If err
is a string, it will be set as the message on an Error
object.
Unlike stub, spy returns the Promise
function by the promised
variable, because the spy
object should be used to do the spy work(checking calls).
// Example with stringvar spy1 = sinon; { return } // Example with Error objectvar spy2 = sinon { return }
Examples
License
MIT © Ben Drucker