Last active
August 29, 2015 14:06
-
-
Save morrissinger/e2cea7c8c647234116b6 to your computer and use it in GitHub Desktop.
Structuring Express Applications for Readability, Unit Testability: Promise Behavior; Tie Routes to Promise States; Test Promise Resolutions
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
//Tie Routes to Promise States | |
var middleware = require('./middleware.js'); | |
app.get('example/uri', function(req, res, next) { | |
middleware(req, res) | |
.then(function() { next(); }) | |
.catch(res.json) | |
.done(); | |
}); |
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
//Promise Behavior | |
var Q = require('q'); | |
module.exports = function(req, res) { | |
var deferred = Q.defer(); | |
// Define middleware behavior and resolve or reject promises accordingly. | |
return deferred.promise; | |
}; |
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
//Test Promise Resolutions | |
var httpMocks = require('node-mocks-http'), | |
chai = require('chai'), | |
chaiAsPromised = require('chai-as-promised'); | |
chai.use(chaiAsPromised); | |
var middleware = require('path/to/middleware'); | |
var req, res; | |
beforeEach(function (done) { | |
req = httpMocks.createRequest(), | |
res = httpMocks.createResponse(); | |
}); | |
describe('middleware', function() { | |
it ('resolves under condition X with result Y', function () { | |
return expect(middleware(req, res)).to.be.fulfilled.then(function() { | |
// Assert | |
}); | |
}); | |
it ('rejects under condition X with error Y', function () { | |
return expect(middleware(req, res)).to.be.rejectedWith('Error String'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment