Skip to content

Commit

Permalink
Added usingPromise method to sandbox.
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 = {
  myMethod: function() { return; }
};

var sandbox = sinon.sandbox.create()

sandbox.stub(myObject);

myObject.myMethod
    .usingPromise(bluebird.Promise)
    .resolves("baz");

myObject.myMethod()
    // Tap should now be available!!!
    .tap(function(actual) {
        assert.strictEqual(actual, "baz");
    });
```

Documentation still to come.
  • Loading branch information
blacksun1 committed Apr 5, 2017
1 parent 17edad7 commit 0a3518f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/sinon/collection.js
Expand Up @@ -71,6 +71,11 @@ var collection = {
return fake;
},

addUsingPromise: function (fake) {
fake.usingPromise(this.promiseLibrary);
return fake;
},

spy: function spy() {
return this.add(sinonSpy.apply(sinonSpy, arguments));
},
Expand All @@ -85,9 +90,16 @@ var collection = {
sinonStub.apply(null, arguments);

if (isStubbingEntireObject) {
collectOwnMethods(stubbed).forEach(this.add.bind(this));
var ownMethods = collectOwnMethods(stubbed);
ownMethods.forEach(this.add.bind(this));
if (this.promiseLibrary) {
ownMethods.forEach(this.addUsingPromise.bind(this));
}
} else {
this.add(stubbed);
if (this.promiseLibrary) {
stubbed.usingPromise(this.promiseLibrary);
}
}

return stubbed;
Expand Down
7 changes: 7 additions & 0 deletions lib/sinon/sandbox.js
Expand Up @@ -90,6 +90,13 @@ extend(sinonSandbox, {
return obj;
},

usingPromise: function (promiseLibrary) {

this.promiseLibrary = promiseLibrary;

return this;
},

restore: function () {
if (arguments.length) {
throw new Error("sandbox.restore() does not take any parameters. Perhaps you meant stub.restore()");
Expand Down
65 changes: 65 additions & 0 deletions test/sandbox-test.js
Expand Up @@ -109,6 +109,71 @@ describe("sinonSandbox", function () {
});
});

describe(".usingPromise", function () {
beforeEach(function () {
this.sandbox = Object.create(sinonSandbox);
});

afterEach(function () {
this.sandbox.restore();
});

it("must be a function", function () {

assert.isFunction(this.sandbox.usingPromise);
});

it("must return the sandbox", function () {
var mockPromise = {};

var actual = this.sandbox.usingPromise(mockPromise);

assert.same(actual, this.sandbox);
});

it("must set all stubs created from sandbox with mockPromise", function () {

var resolveValue = {};
var mockPromise = {
resolve: sinonStub.create().resolves(resolveValue)
};

this.sandbox.usingPromise(mockPromise);
var stub = this.sandbox.stub().resolves();

return stub()
.then(function (action) {

assert.same(resolveValue, action);
assert(mockPromise.resolve.calledOnce);
});
});

it("must set all stubs created from sandbox with mockPromise", function () {

var resolveValue = {};
var mockPromise = {
resolve: sinonStub.create().resolves(resolveValue)
};
var stubbedObject = {
stubbedMethod: function () {
return;
}
};

this.sandbox.usingPromise(mockPromise);
this.sandbox.stub(stubbedObject);
stubbedObject.stubbedMethod.resolves({});

return stubbedObject.stubbedMethod()
.then(function (action) {

assert.same(resolveValue, action);
assert(mockPromise.resolve.calledOnce);
});
});
});

// These were not run in browsers before, as we were only testing in node
if (typeof window !== "undefined") {
describe("fake XHR/server", function () {
Expand Down

0 comments on commit 0a3518f

Please sign in to comment.