Skip to content

Commit

Permalink
Added usingPromise method to stub.
Browse files Browse the repository at this point in the history
The `usingPromise` method allows the setting of the promise library
that will be used by the resolve and reject method. If it is not set
then it will use the default promise implementation by default.

Example:

```js
var assert = require("assert");
var bluebird = require("bluebird");
var sinon = require("sinon");

var myObject = {};
var myStub = sinon.stub()
    .usingPromise(bluebird.Promise)
    .resolves(myObject);

myStub()
    // Tap should now be available!!!
    .tap(function(actual) {
        assert.strictEqual(actual, myObject);
    })
    .catch(function(err) {
        console.err("Error", err);
    })
```

Documentation still to come.
  • Loading branch information
blacksun1 authored and blacksun1 committed Apr 5, 2017
1 parent c5bc9ab commit 17edad7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/sinon/behavior.js
Expand Up @@ -129,9 +129,9 @@ var proto = {
} else if (this.fakeFn) {
return this.fakeFn.apply(context, args);
} else if (this.resolve) {
return Promise.resolve(this.returnValue);
return (this.promiseLibrary || Promise).resolve(this.returnValue);
} else if (this.reject) {
return Promise.reject(this.returnValue);
return (this.promiseLibrary || Promise).reject(this.returnValue);
} else if (this.callsThrough) {
return this.stub.wrappedMethod.apply(context, args);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/sinon/default-behaviors.js
Expand Up @@ -68,6 +68,10 @@ module.exports = {
fake.callbackAsync = false;
},

usingPromise: function usingPromise(fake, promiseLibrary) {
fake.promiseLibrary = promiseLibrary;
},

yields: function (fake) {
fake.callArgAt = useLeftMostCallback;
fake.callbackArguments = slice.call(arguments, 1);
Expand Down
53 changes: 53 additions & 0 deletions test/stub-test.js
Expand Up @@ -317,6 +317,59 @@ describe("stub", function () {
});
});

describe(".usingPromise", function () {
it("should exist and be a function", function () {
var stub = createStub.create();

assert(stub.usingPromise);
assert.isTrue(typeof stub.usingPromise === "function");
});

it("should return the current stub", function () {
var stub = createStub.create();

assert.same(stub.usingPromise(Promise), stub);
});

it("should set the promise used by resolve", function () {
var stub = createStub.create();
var promise = {
resolve: createStub.create().callsFake(function (value) {
return Promise.resolve(value);
})
};
var object = {};

stub.usingPromise(promise).resolves(object);

return stub().then(function (actual) {
assert.same(actual, object, "Same object resolved");
assert.isTrue(promise.resolve.calledOnce, "Custom promise resolve called once");
assert.isTrue(promise.resolve.calledWith(object), "Custom promise resolve called once with expected");
});
});

it("should set the promise used by reject", function () {
var stub = createStub.create();
var promise = {
reject: createStub.create().callsFake(function (err) {
return Promise.reject(err);
})
};
var reason = new Error();

stub.usingPromise(promise).rejects(reason);

return stub().then(function () {
referee.fail("this should not resolve");
}).catch(function (actual) {
assert.same(actual, reason, "Same object resolved");
assert.isTrue(promise.reject.calledOnce, "Custom promise reject called once");
assert.isTrue(promise.reject.calledWith(reason), "Custom promise reject called once with expected");
});
});
});

describe(".throws", function () {
it("throws specified exception", function () {
var stub = createStub.create();
Expand Down

0 comments on commit 17edad7

Please sign in to comment.