javascript - Unit Testing with ExpresJS (Confused) -
so trying unit test expresjs routes. looks somthing
server.js
var boards = require('./routes/boardsroute.js'); app.get('/api/v1/boards/:id', boards.getboard);
boardroutes.js
exports.getboard = function(req, res) { boardmodel.find({ name: 'kanban board' }, function(err, columns) { if (!err) { return res.send(columns); } else { return console.log(err); } }); return res.send(board); };
i mock out boardmodel call mongoose model (aka database call). assume unit tests should not making calls database , have no server running.
should testing getboards seperatly server.js app.get() call. (as these requests apt.get covered integration tests/e2e tests , http requests)
all documentation , frameworks can see either have have express server running in order unit test route , particular exports.getboard.
things have tried ,
use sinon.js mock fakeserver test http request , method getboard.
use superagent , super test make requests out server. (i uncomfortable unit tests should not have server runnig).
i trying use mocha test these routes.
any great clear confusion.
i test route wired correctly first, test call route expect.
to test route here's 1 way it. create routes module wires routes:
var routes = {}; routes.create = function(app) { app.get('/api/v1/boards/:id', boards.getboard); } module.exports = routes;
you can mock app object , check method gets called correctly. (i tend use sinon.js this.) create method need called app module:
var app = express(); ... // set express routes.create(app);
now can focus on testing getboard on own other module.
Comments
Post a Comment