找到了关于如何在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
那永远不会失败。
如果运行您的示例,您将看到错误堆栈跟踪但测试通过,因为:
- 它在测试期间没有发现错误。
- 没有错误信息传递给
done()
函数调用。
我的修复在 PR #2053 中:
- 使用
expect()
将断言错误返回给.end()
。 - 将任何错误信息传递给
done()
。
关于javascript - 超测测试 express 中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22816121/