lundi 2 février 2015

Unit testing express routers

I know this has been discussed a couple of times. Anyway, I feel like all the provided solutions don't (perfectly) fit to my requirement(s). I have the following code:



router.js:
------------------
var Router = function(app, resourceName, controller) {
//Create
app.post('/api/' + resourceName, function(req, res) {
console.log('Incoming request: ' + resourceName + ' (POST)');
controller.create(req, res);
});
};

module.exports = Router;


As you can see this is a very "generic" router. It can be instantiated for example in the server like this:



var app = express();
var userController = ...
var userRouter = new Router(app, 'Users', userController);


So I don't have to write a file per resource but I just have one generic router.


I would like to test my generic router but I see some problems:



  1. How to "inject" the app? I could create an instance of Express (var app = express();) but I think a mock would be better (as this is a unit test, not an integration test!). What's the best way to get an appropriate mock?

  2. What exactly should I test? As far as I see my router itself (without integration) isn't doing anything else but console output (not worth to test) and a call of a function (controller.create(req, res);). How should I test if this function is called? Or is there anything else to test?


Aucun commentaire:

Enregistrer un commentaire