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/integration test for a route.
Being able to unit test simple modules is easy and have been doing it with Mocha; however, my unit tests with Express fail since the response object I'm passing in doesn't retain the values.
Route-Function Under Test (routes/index.js):
exports.index = function(req, res){
res.render('index', { title: 'Express' })
};
Unit Test Module:
var should = require("should")
, routes = require("../routes");
var request = {};
var response = {
viewName: ""
, data : {}
, render: function(view, viewData) {
viewName = view;
data = viewData;
}
};
describe("Routing", function(){
describe("Default Route", function(){
it("should provide the a title and the index view name", function(){
routes.index(request, response);
response.viewName.should.equal("index");
});
});
});
When I run this, it fails for "Error: global leaks detected: viewName, data".
Where am I going wrong so that I can get this working?
Is there a better way for me to unit test my code at this level?
Update
1. Corrected code snippet since I initially forgot "it()".
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…