Skip to content

Instantly share code, notes, and snippets.

@joeeames
Created May 22, 2015 23:07
Show Gist options
  • Save joeeames/114c2b8da4b87fc75123 to your computer and use it in GitHub Desktop.
Save joeeames/114c2b8da4b87fc75123 to your computer and use it in GitHub Desktop.
'use strict';
var Promise = require("bluebird")
, chai = require("chai")
, chaiAsPromised = require('chai-as-promised')
, should = chai.should()
;
chai.use(chaiAsPromised);
var student = { name: "John Doe", id: 3 }
var dataAccess = {
getStudent: function(id) {
if(id === 3) {
return Promise.resolve(student);
} else {
return Promise.reject('Invalid Student Id')
}
}
}
describe("getStudent", function () {
it("uses the done function", function (done) {
dataAccess.getStudent(3).then(function (retval) {
retval.id.should.equal(3);
done();
});
});
//this one works!
it("fulfills the promise", function () {
return dataAccess.getStudent(3);
});
//make the former into this
it("fullfills with the correct student", function () {
return dataAccess.getStudent(3).should.eventually.equal(student);
});
});
'use strict';
var Promise = require("bluebird")
, chai = require("chai")
, should = chai.should()
;
function returnPromise () {
return Promise.resolve(3);
}
function returnRejectedPromise () {
return Promise.reject("I failed!");
}
describe("testing with promises", function () {
//test passes, but you get 'Unhandled rejection AssertionError' because the should
// fires after the test returns.
xit("doesn't use done function", function () {
returnPromise().then(function () {
should.not.exist(true);
});
});
//times out because the done function doesn't work this way?
it("uses the done function", function (done) {
returnPromise().then(function (retval) {
retval.should.equal(3);
done();
});
});
//this one works!
it("returns the promise", function () {
return returnPromise().then(function (retval) {
retval.should.equal(3); // change this to 4 to show that it works
});
});
//because we're returning the promise, a rejected promise will fail the test
it("fails because of rejected promise", function () {
return returnRejectedPromise();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment