Skip to content

Instantly share code, notes, and snippets.

@bryanforbes
Last active March 11, 2016 15:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanforbes/11151281 to your computer and use it in GitHub Desktop.
Save bryanforbes/11151281 to your computer and use it in GitHub Desktop.
define([
'intern!object',
'intern/chai!expect',
'sinon',
'when',
'intern/chai!',
'sinon-chai'
], function (registerSuite, expect, sinon, promise, chai, sinonChai) {
// add sinon assertions to chai
chai.use(sinonChai);
function promiseThatResolves() {
return promise.promise(function(resolve, reject) { reject(42); });
}
function promiseThatRejects() {
return promise.promise(function(_, reject) { reject('A rejected promise'); });
}
function functionThatCallsMockedDependency(externalDependency) {
return promise.promise(function(resolve) {
externalDependency.method();
resolve();
});
}
registerSuite({
name: 'promises and intern',
'resolving promises': function () {
// This will implicitly handle the case where the promise rejects
return promiseThatResolves().then(function (value) {
// If the expectation isn't met, it throws and the promise
// rejects, which fails the test
expect(value).to.equal(42);
});
},
'a rejected promise example': function () {
return promiseThatRejects().then(
function () {
throw new Error('should not have resolved');
},
function (value) {
expect(value).to.equal('A rejected promise');
}
);
},
'testing for a mock-call': function () {
var dfd = this.async(),
myMock = { method: sinon.spy() };
functionThatCallsMockedDependency(myMock).then(
// dfd.callback returns a function that executes the callback passed
// like so:
// try { callback(value); dfd.resolve(); }
// catch(e) { dfd.reject(e); }
dfd.callback(function (value) {
expect(myMock.method).to.have.been.called;
}),
dfd.reject
);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment