Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a module to easily mock req/res objects for unit testing a connect style handler?

I'm writing an app in node.js that includes some connect style endpoint handlers ( function(req,resp) ) and would like to write some unit tests against them without requiring the full app to be running.

I know I can "simply" push whatever fixture I manually write into it, but I was wondering if there is any library out there to help me generate these fixtures faster.

EDIT: to further explain what i want, I would like to in my unit test execute my handler only (not my app) and for that, i'd need a fake req and res. Those are the 2 objects I'd like to mock.

I'm currently using mocha as the test runner and the core assert module.

like image 378
Miguel Coquet Avatar asked Dec 28 '12 15:12

Miguel Coquet


1 Answers

If you defined your routes in a way where you pass app to a function then you could use supertest to test a route.

Test

var app = require('./real-or-fixture-app'); //depends on your setup

require('routeToTest')(app);

var request = require("supertest");

describe("Test", function(){
    it("should test a route", function(done){
        request(app)
            .post("/route")
            .send({data:1})
            .expect(200, done);
    });
});

Route definition

module.exports = function(app){
    app.get("/route", ....
};

I am not quite sure that this is really what you are looking for, but it is a way to test your routes separately.

like image 190
topek Avatar answered Oct 05 '22 23:10

topek