I\'m in the process of learning Node.js and have been playing around with Express. Really like the framework;however, I\'m having trouble figuring out how to write a unit/i
The easiest way to test HTTP with express is to steal TJ's http helper
I personally use his helper
it("should do something", function (done) {
request(app())
.get('/session/new')
.expect('GET', done)
})
If you want to specifically test your routes object, then pass in correct mocks
describe("Default Route", function(){
it("should provide the a title and the index view name", function(done){
routes.index({}, {
render: function (viewName) {
viewName.should.equal("index")
done()
}
})
})
})
I've come to the conclusion that the only way to really unit test express applications is to maintain a lot of separation between the request handlers and your core logic.
Thus, your application logic should be in separate modules that can be require
d and unit tested, and have minimal dependence on the Express Request and Response classes as such.
Then in the request handlers you need to call appropriate methods of your core logic classes.
I'll put an example up once I've finished restructuring my current app!
I guess something like this? (Feel free to fork the gist or comment, I'm still exploring this).
Edit
Here's a tiny example, inline. See the gist for a more detailed example.
/// usercontroller.js
var UserController = {
_database: null,
setDatabase: function(db) { this._database = db; },
findUserByEmail: function(email, callback) {
this._database.collection('usercollection').findOne({ email: email }, callback);
}
};
module.exports = UserController;
/// routes.js
/* GET user by email */
router.get('/:email', function(req, res) {
var UserController = require('./usercontroller');
UserController.setDB(databaseHandleFromSomewhere);
UserController.findUserByEmail(req.params.email, function(err, result) {
if (err) throw err;
res.json(result);
});
});