1
我的快速应用程序中有一些中间件功能,它向外向服务器发出传出请求,然后将响应发送回客户端。Express中的中间件测试请求响应
所以这有点儿事:
var request = require('request'),
helpers = require('./helpers.js');
//GET
module.exports.fetch = function(req, res) {
var options = helpers.buildAPIRequestOptions(req);
request(options, function(err, response, body){
res.status(response.statusCode).send(body);
});
};
我期待在编写单元测试,并想模拟出当withing这些功能的传出请求被炒鱿鱼回到我自己的反应。
我所遇到supertest并在写一套房,像这样有一展身手:
var app = require('./index.js'),
request = require('supertest')(app),
should = require('should');
//GET
describe('GET requests', function(){
it('should do something', function(done){
request
.get('/api/schemes')
.expect(200, done)
.end(function(err, res){
// console.log(res);
// console.log(err);
res.status.should.equal(200);
});
});
});
我想,我需要这样的东西兴农这里,这样我就可以窥探会打我的中间件功能的请求并返回我选择的答复。我只是在努力去理解如何在我的测试中添加残片。
有人可以提出进一步的建议吗?
由于
nock是我用来嘲讽外部网络请求 –
感谢您的建议,它看起来像是有用的东西,只是要弄清楚如何使用我在OP中共享的示例! :) – mindparse
@mindparse抛出'nock.recorder.rec();'在请求之前,你会得到代码来模拟你刚才在控制台中得到的响应。 – Gant