表达中间件测试摩卡车 [英] express middleware testing 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();
}
}
我发现的唯一中间件测试是:
module.exports =函数(请求,响应,下一个){
/ *
*执行REQUEST或RESPONSE
** /
if(!request.didSomething){
console.log(dsdsd);
request.didSomething = true;
next();
} else {
//发生错误,抛出和错误
var error = new Error();
error.message ='执行此操作时出错'
next(error);
}
};
describe('Middleware test',function(){
上下文('有效参数传递',function(){
beforeEach功能(完成){
/ *
*在每次测试之前,将REQUEST和RESPONSE变量
*重新发送到中间件
** /
请求= httpMocks.createRequest({
method:'GET',
url:'/css/main.css',
query:{
myid:'312'
}
});
respond = httpMocks.createResponse();
done(); //调用完成,以便下一个测试可以运行
}) ;
它('做某事',功能(完成){
/ *
*中间件希望被传递3个参数:请求,响应和下一个
*我们将手动通过REQUEST和RESPONS E进入中间件
*并为下一个创建函数回调我们运行我们的测试
** /
中间件(响应,响应,功能下一个(错误){
/ *
*通常,除了错误之外,我们不会将任何内容传递给下一个,所以因为在这个测试中
*我们传递了REQUEST中的有效数据,所以我们不应该得到一个
*错误被传递在
** /
if(error){throw new Error('Expected not to receive a error');
//其他测试对请求和响应
if(!responses.didSomething){throw new Error('Expected something to be done'); }
done(); //调用完成,所以我们可以运行下一个测试
}); //关闭中间件
}); //关闭它
}); // close context
}); //关闭描述
这个工作与简单的中间件(它喜欢测试基本功能与回调)以上,但更复杂的中间件我不能得到它的工作。是否有可能测试这种中间件?
这是一个简单的设置,您可以使用 chai
和 sinon
:
var expect = require('chai')。
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(){$ ($($($)
'应该返回一个函数()',function(){
expect(mw).to.be.a.Function;
});
它('应该接受三个参数',function(){
expect(mw.length).to.equal(3);
});
});
describe 'request handler call',function(){
it('should call next()once',function(){
var mw = middleware(/./);
var nextSpy = sinon.spy();
mw({},{},nextSpy);
expect(nextSpy.calledOnce).to.be.true;
});
});
描述('模式测试',function(){
...
});
});
从那里,您可以为模式匹配等添加更精细的测试。由于您只是使用 req.url
,您不必嘲弄整个请求
对象(由Express创建)和您只需使用一个简单的对象,一个 url
属性。
Is there a way to test those kind of middleware in express:
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();
}
}
The only middleware testing i found was:
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
This work well with the simple middleware (it like testing basic function with callback) provided above but with more complex middleware i cannot get it work. Is it possible to test this kind of middleware?
Here's a simple setup that you could use, using chai
and 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() {
...
});
});
From there, you can add more elaborate tests for the pattern matching, etc. Since you're only using req.url
, you don't have to mock an entire Request
object (as created by Express) and you can just use a simple object with a url
property.
这篇关于表达中间件测试摩卡车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!