Testing router functions
Now that we've seen a little bit about testing a data model, let's move on to testing the route functions. There is obviously a lot more to test in the Notes data models, such as the user model. Our goal in this chapter is not to produce a complete test suite, but to introduce you to unit testing in Node.
In unit testing the route functions, we have a little more freedom in mocking out the other modules. We can fairly easily create a faux data model, and can even bypass Express completely. Let's see how it's done.
Let's start with the faux data model. It can also be used as an in-memory data model in the Notes application, if you like. In the test
directory, create a file named mock-model.js
containing the following:
var events = require('events'); var emitter = module.exports.emitter = new events.EventEmitter(); var notes = []; exports.connect = function(params, callback) { callback(); } exports.disconnect = function(callback) { callback(); } exports.update...