2015-12-29 16 views
7

Có cách nào để kiểm tra những loại trung gian trong nhanh:middleware nhanh thử nghiệm mocha chai

module.exports = function logMatchingUrls(pattern) { 
    return function (req, res, next) { 
     if (pattern.test(req.url)) { 
      console.log('request url', req.url); 
      req.didSomething = true; 
     } 
     next(); 
    } 
} 

Việc thử nghiệm trung duy nhất tôi tìm thấy là:

module.exports = function(request, response, next) { 
    /* 
    * Do something to REQUEST or RESPONSE 
    **/ 

    if (!request.didSomething) { 
     console.log("dsdsd"); 
     request.didSomething = true; 
     next(); 
    } else { 
     // Something went wrong, throw and error 
     var error = new Error(); 
     error.message = 'Error doing what this does' 
     next(error);   
    } 
}; 


describe('Middleware test', function(){ 

    context('Valid arguments are passed', function() { 
     beforeEach(function(done) { 
      /* 
      * before each test, reset the REQUEST and RESPONSE variables 
      * to be send into the middle ware 
      **/ 
      requests = httpMocks.createRequest({ 
       method: 'GET', 
       url: '/css/main.css', 
       query: { 
        myid: '312' 
       } 
      }); 
      responses = httpMocks.createResponse(); 

      done(); // call done so that the next test can run 
     }); 

     it('does something', function(done) { 
      /* 
      * Middleware expects to be passed 3 arguments: request, response, and next. 
      * We are going to be manually passing REQUEST and RESPONSE into the middleware 
      * and create an function callback for next in which we run our tests 
      **/ 
      middleware(responses, responses, function next(error) { 
       /* 
       * Usually, we do not pass anything into next except for errors, so because 
       * in this test we are passing valid data in REQUEST we should not get an 
       * error to be passed in. 
       **/ 
       if (error) { throw new Error('Expected not to receive an error'); } 

       // Other Tests Against request and response 
       if (!responses.didSomething) { throw new Error('Expected something to be done'); } 

       done(); // call done so we can run the next test 
      }); // close middleware 
     }); // close it 
    }); // close context 
}); // close describe 

Công việc này tốt với các trung gian đơn giản (nó giống như thử nghiệm chức năng cơ bản với gọi lại) được cung cấp ở trên nhưng với middleware phức tạp hơn tôi không thể làm cho nó hoạt động. Có thể thử nghiệm loại middleware này không?

Trả lời

13

Dưới đây là một thiết lập đơn giản mà bạn có thể sử dụng, sử dụng và chaisinon:

var expect = require('chai').expect; 
var sinon = require('sinon'); 

var middleware = function logMatchingUrls(pattern) { 
    return function (req, res, next) { 
     if (pattern.test(req.url)) { 
      console.log('request url', req.url); 
      req.didSomething = true; 
     } 
     next(); 
    } 
} 

describe('my middleware', function() { 

    describe('request handler creation', function() { 
    var mw; 

    beforeEach(function() { 
     mw = middleware(/./); 
    }); 

    it('should return a function()', function() { 
     expect(mw).to.be.a.Function; 
    }); 

    it('should accept three arguments', function() { 
     expect(mw.length).to.equal(3); 
    }); 
    }); 

    describe('request handler calling', function() { 
    it('should call next() once', function() { 
     var mw  = middleware(/./); 
     var nextSpy = sinon.spy(); 

     mw({}, {}, nextSpy); 
     expect(nextSpy.calledOnce).to.be.true; 
    }); 
    }); 

    describe('pattern testing', function() { 
    ... 
    }); 

}); 

Từ đó, bạn có thể thêm các xét nghiệm phức tạp hơn cho phù hợp với mô hình, vv Kể từ khi bạn chỉ sử dụng req.url, bạn không phải thử toàn bộ đối tượng Request (như được tạo bởi Express) và bạn chỉ có thể sử dụng một đối tượng đơn giản với thuộc tính url.

Các vấn đề liên quan