javascript - 超测测试 express 中间件

标签 javascript node.js testing express supertest

找到了关于如何在express中测试中间件的提示:
https://github.com/visionmedia/express/blob/master/test/req.xhr.js
我想知道为什么我的测试总是通过。直到我注意到当我从 express 复制测试时,它们的行为是一样的。我试着把它们搞砸了,但它们一直在通过: https://github.com/visionmedia/express/blob/master/test/req.xhr.js

我在这里缺少什么?

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();
  })
})

最佳答案

你什么都没漏,就是快测req.xhr那永远不会失败。

如果运行您的示例,您将看到错误堆栈跟踪但测试通过,因为:

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

我的修复在 PR #2053 中:

  1. 使用expect()将断言错误返回给.end()
  2. 将任何错误信息传递给 done()

关于javascript - 超测测试 express 中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22816121/

相关文章:

javascript - Angularjs 从 http url 中的服务数据中过滤错误

javascript - 防止使用后退按钮(在 IE 中)

android - 如何通过appium执行模拟器命令

testing - 我如何像真正的测试人员一样系统地测试和思考

javascript - Jest Mock React 组件仅在某些测试中

javascript - 语法错误 : Unexpected token in object literal in Google Apps script

node.js - 如何通过 Accumulo 代理客户端基于 rowkey 删除 Accumulo 中的记录

angularjs - 打开已部署的仪表板时遇到问题

node.js - Appium Grid hub 向 Node 发送错误的功能集

eclipse - 如何在 Eclipse 中轻松创建单元测试