How to unit test express Router calls specific controller?

1,063 views
Skip to first unread message

Justin Maat

unread,
Mar 11, 2015, 12:25:48 PM3/11/15
to expre...@googlegroups.com
Sorry if this is a ridiculous question and I'm overlooking something simple.  But, I can't figure out a way to unit test that a router file maps an endpoint string to a specific controller method.

I see alot of examples on how to test the api responses, but nothing about testing the actual router itself (if abstracted into separate files).

I'm trying to test that my express app routes are directed to the correct controller methods.

For example -

//server.js, base application

var express = require("express");
var app = express();
require("./routes.js")(app);
...

//routes.js
var menuController = require("./controllers/menu.js");

module.exports = function(expressApp) {
    expressApp.get('/menu', menuController.getMenu);
};
...

//test file
var express = require('express')
    , menuController = require("../../controllers/menu.js")
    , chai = require('chai')
    , should = chai.should()
    , sinon = require('sinon')
    , sinonChai = require("sinon-chai");
chai.use(sinonChai);

var app = express();
require("../../routes/routes.js")(app);

describe('routes.js', function(){

    it('/menu should call menuController.getMenu route', function(){
        var spy = sinon.spy(menuController, 'getMenu');
        app.get('/menu', spy);

        spy.should.have.been.called;  //fails, never called
    });

});  

How can I check to see that when calling app.get('/menu', ..), the callback from menuController is invoked?  Should I restructure the app somehow (I see a bunch of other ways to configure the routing)?

Message has been deleted

Justin Maat

unread,
Mar 11, 2015, 12:32:42 PM3/11/15
to expre...@googlegroups.com
I just moved this to the newer Express 4 Router, but the question remains the same.  http://expressjs.com/api.html#router



I also posted this on SO, hoping someone might be able to give some guidance.  

Jason Crawford

unread,
Mar 11, 2015, 1:18:56 PM3/11/15
to expre...@googlegroups.com
For what it's worth, I don't bother to unit-test routes. Routes are glue logic. I cover them in integration tests.

If you hit your API and the right response is served up, then your routes are working. No real need to unit-test them.

-Jason

--
Blog: http://blog.jasoncrawford.org  |  Twitter: @jasoncrawford



--
You received this message because you are subscribed to the Google Groups "Express" group.
To unsubscribe from this group and stop receiving emails from it, send an email to express-js+...@googlegroups.com.
To post to this group, send email to expre...@googlegroups.com.
Visit this group at http://groups.google.com/group/express-js.
For more options, visit https://groups.google.com/d/optout.

Justin Maat

unread,
Mar 11, 2015, 1:31:42 PM3/11/15
to expre...@googlegroups.com
I actually used to think that way as well, but I'm starting to see a benefit to unit testing the routes mapping files directly.  

Take this as an example - someone (probably me :)  changes a controller method name, but doesn't refactor it throughout the codebase.  An integration test would catch it, but depending how you've set it up, it may not be clear on where the error was.  By truly unit testing it, I can run the tests very quickly and ensure all the mappings are as I expected them to be.  Also, if using test-code coverage tools, you'll need to specifically ignore the routing files (we do code coverage on our unit tests package).

But... I do see your point.  When the router file is abstracted away, it feels more like some type of properties file (in a way). 


Reply all
Reply to author
Forward
0 new messages