Last active
March 11, 2016 15:52
-
-
Save bryanforbes/11151281 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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