So far, we have not done any testing. This is bad and not a best practice. We will rectify this here. We will only build a few tests, but it will demonstrate how to create tests. Create a directory named tests
and create the routes.js
file.
Open the file and paste the following code:
var routes = require('../routes'), config = require('../config'), nodeunit = require('nodeunit'), Request = require('./request'), Response = require('./response'); exports.indexRouteTest = function(test){ var res = new Response(); test.equal(res.view, undefined); routes.index({}, res); test.equal(res.view, 'index'); test.equal(res.viewData.title, 'Index'); test.done(); };
Here, we are testing the index route. It should call render on the response object passing in index as the view and an object that has title
set to Index
. To test this, we are not going to set up an entire Node.js and Express stack. We are just going to mock up what we need. We need a response object, so...