这里有一个简单的设置,你可以使用,使用
chai和
sinon:
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() { ... });});从那里,您可以为模式匹配等添加更多详细的测试。由于仅使用
req.url,因此不必模拟整个
Request对象(由Express创建),而只需使用带有
url属性的简单对象。



