OK, first, although testing your routing code is something you may or may not want to do, in general, try to separate your interesting business logic from pure javascript code (classes or functions) that are separated from explicit or any other structure using and use mocha vanilla tests to test this. Once you achieve this, if you want to really check the routes that you configure in mocha, you need to pass the mock req, res parameters to your middleware functions to mimic the interface between the express connection and your middleware.
For a simple case, you can create a mock res object with a render function that looks something like this.
describe 'routes', -> describe '#show_create_user_screen', -> it 'should be a function', -> routes.show_create_user_screen.should.be.a.function it 'should return something cool', -> mockReq = null mockRes = render: (viewName) -> viewName.should.exist viewName.should.match /createuser/ routes.show_create_user_screen(mockReq, mockRes).should.be.an.object
Also, just the FYI middleware functions should not return any particular value, this is what they do with the req, res, next parameters that you should focus on when testing.
Here are some javascript as you requested in the comments.
describe('routes', function() { describe('#show_create_user_screen', function() { it('should be a function', function() { routes.show_create_user_screen.should.be.a["function"]; }); it('should return something cool', function() { var mockReq = null; var mockRes = { render: function(viewName) { viewName.should.exist; viewName.should.match(/createuser/); } }; routes.show_create_user_screen(mockReq, mockRes); }); }); });
Peter Lyons Jan 13 2018-12-12T00: 00Z
source share