jeudi 24 décembre 2015

Unit testing Express router in Mocha

This is my code:

router.get('/', function(req, res) {
    RESTCalls_GET.getAllSoftwareComponentsInformation(function(allSoftwareComponents_Returned) {
        if(ErrorCheck.ErrorCheck_Object(allSoftwareComponents_Returned)) {
            ErrorHandle.RenderErrorPage(allSoftwareComponents_Returned, res);
        } else {
            RESTCalls_GET.getJIRAProjectAndAssociatedSoftwareComponents(function(projectReturn) {
                if(ErrorCheck.ErrorCheck_Object(projectReturn)) {
                    ErrorHandle.RenderErrorPage(projectReturn, res);
                } else {
                    RESTCalls_GET.getAllSoftwareComponentIDsAndJIRAFiltersTheyUse(function(filterReturn) {
                        if(ErrorCheck.ErrorCheck_Object(filterReturn)) {
                            ErrorHandle.RenderErrorPage(filterReturn, res);
                        } else {
                            res.render('softwareComponentList_View', {
                                projects:   projectReturn,
                                filters:    filterReturn,
                                products:   allSoftwareComponents_Returned
                            });
                        }
                    });
                }
            });
        }
    });
});

module.exports = {
    Router: router
};

I'm relatively new to Mocha (and unit testing in general) but I have figured out how to test simple functions already. How would I go about testing this router calling several functions, each with a callback, and rendering a view at the end?

I have looked at this question for some insight to the question but the main difference is that my .get has a callback.

This is what I have tried so far (does not work):

var request = {};
var response = {
    viewName: "",
    data: {},
    render: function(view, viewData) {
        this.viewName = view;
        this.data = viewData;
    }
};

describe("Routing", function() {
    describe('router.get()', function() {
        it("should provide the name of the view it's rendering", function(done) {
            SCList_Controller.Router.get('', function(request, response) {
                response.viewName.should.equal('softwareComponentList_View');
                done();
            });
        });
    });
});

Any help appreciated.

Aucun commentaire:

Enregistrer un commentaire