最优测试中间件 [英] supertest test express middleware

查看:161
本文介绍了最优测试中间件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发现以下提示如何在快速测试中间件:

https://github.com/visionmedia/express/blob/master/test/req.xhr.js
我想知道为什么我的测试总是过去。直到我注意到,当我从快递复制测试时,他们的行为是一样的。我试着拧紧它们,但是他们继续传递:
https ://github.com/visionmedia/express/blob/master/test/req.xhr.js


我在这里缺少什么?

 它(当X-Requested-With为xmlhttprequest时应该返回true),function(done){
var app = express();

app.use(function(req,res){
req.xhr.should.be.false; //设置为false,测试失败但仍然通过
res




$($ / $)
.set('X-Requested-With' 'xmlhttprequest')
.end(function(res){
done();
})
})


解决方案

你没有错过任何东西,这是快速测试 req.xhr 永远不会失败。 >

如果运行你的例子,你会看到错误堆栈跟踪,但测试通过,因为:


  1. 它没有在测试期间捕获错误。 li>
  2. 没有错误信息传递给 done()函数调用。

我的修复在 PR#2053


  1. 使用 expect()将断言错误返回到 .end()

  2. 将任何错误信息传递给 done()


found the following hint on how to test middleware in express:
https://github.com/visionmedia/express/blob/master/test/req.xhr.js
I was wondering why my tests were always passing. Until I noticed that when i copied the test from express they behaved the same. I tried screwing them up but they keep passing: https://github.com/visionmedia/express/blob/master/test/req.xhr.js

What am I missing here?

it('should return true when X-Requested-With is xmlhttprequest', function(done){
  var app = express();

  app.use(function(req, res){
    req.xhr.should.be.false; //set to false, to fail the test but it still passes
    res.end();
  });

  request(app)
  .get('/')
  .set('X-Requested-With', 'xmlhttprequest')
  .end(function(res){
    done();
  })
})

解决方案

You didn't miss anything, it is the express test req.xhr that will never fail.

If running your example, you will see the error stacktrace but the test passes because:

  1. it didn't catch the error during test.
  2. no error information passed to done() function call.

My fixes are in PR #2053:

  1. use expect() to return the assertion error to .end().
  2. pass any error information to done().

这篇关于最优测试中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆