mercredi 12 février 2020

How to test catch 404 from middleware in expressJS?

I am testing a middleware, the following is my code. I want to confirm whether the middleware would respond 404 upon error. But I think I have done the test wrongly.

var Q = require('q');

function TryMid(provider) {
    this.provider = provider;
}

 TryMid.prototype.list = function(req, res) {
     var category = req.params.category;
     var scope = req.params.scope.split(',');
     var limit = scope[1];
     var offset = scope[0];
     return this.provider.doList(category, { offset: offset, limit: limit }).then(function(result) {
         res.status(200).json(result);
     }).catch(function(err) {
         res.status(404).end();
     });
 }

 module.exports = TryMid;

~
The following is my test

 var httpMocks = require('node-mocks-http');
 var should = require('should');

describe('Test TryMid', function() {

    var trymid = new TryMid();
    it('can respond nothing found', function(done) {
       var request = httpMocks.createRequest({
         method: 'GET',
         url: '/mid/try/0,10',
         params: {
             category: 'try',
             scope: '0,10'
         }
     }); 
     var response = httpMocks.createResponse();
     trymid.list(request, response).then(function(result) {
         response.statusCode.should.equal(404);
         done();
     });
   });
});

Please point out the problem. Thank you very much

Aucun commentaire:

Enregistrer un commentaire