mardi 21 février 2017

Unit testing an Express controller with testdouble

I have a route to upload a file and I'm using the Multer to handle multipart uploads. What I try to do is a unit test. So I mock both req and res and wish to correctly use the testdouble.js library to handle upload

exports.uploadFile = function(req, res) {
  upload(req, res, function(err) {
    if(err) {
      res.status(400).send(err);
      return;
    }

    if(!req.file){
      res.status(400).send("No file passed");
      return;
    }

    ...
    ...

Here is my test

var req  = httpMocks.createRequest();
var res = httpMocks.createResponse();

var upload = td.function();
td.when(upload(req, res)).thenCallback("error");

controller.uploadExcelFileToCreateUserAccounts(req, res);

expect(res._getData()).to.equal("error");
expect(res.statusCode).to.equal(400);

But err is undefined when I run it. Any ideas?

Aucun commentaire:

Enregistrer un commentaire