node.js 如何使用 superagent/supertest 链接 http 调用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21089842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to chain http calls with superagent/supertest?
提问by Tim
I am testing an express API with supertest.
我正在使用 supertest 测试快速 API。
I couldn't get multiple requests in a test case to work with supertest. Below is what i tried in a test case. But the test case seem to only execute the last call which is the HTTP GET.
我无法在一个测试用例中收到多个请求来使用 supertest。以下是我在测试用例中尝试的内容。但是测试用例似乎只执行最后一个调用,即 HTTP GET。
it('should respond to GET with added items', function(done) {
var agent = request(app);
agent.post('/player').type('json').send({name:"Messi"});
agent.post('/player').type('json').send({name:"Maradona"});
agent.get('/player').set("Accept", "application/json")
.expect(200)
.end(function(err, res) {
res.body.should.have.property('items').with.lengthOf(2);
done();
});
);
Anything i am missing here, or is there another way to chain http calls with superagent?
我在这里遗漏了什么,还是有另一种方法可以将 http 调用与 superagent 链接起来?
采纳答案by harun
The calls are made asynchronous, so you need to use callback functions to chain them.
调用是异步的,因此您需要使用回调函数来链接它们。
it('should respond to GET with added items', function(done) {
var agent = request(app);
agent.post('/player').type('json').send({name:"Messi"}).end(function(){
agent.post('/player').type('json').send({name:"Maradona"}).end(function(){
agent.get('/player')
.set("Accept", "application/json")
.expect(200)
.end(function(err, res) {
res.body.should.have.property('items').with.lengthOf(2);
done();
});
});
});
});
回答by Tim
Tried to put this in a comment above, formatting wasn't working out.
试图将其放在上面的评论中,格式不起作用。
I'm using async, which is reallystandard and works really well.
我正在使用异步,这是非常标准的并且运行良好。
it('should respond to only certain methods', function(done) {
async.series([
function(cb) { request(app).get('/').expect(404, cb); },
function(cb) { request(app).get('/new').expect(200, cb); },
function(cb) { request(app).post('/').send({prop1: 'new'}).expect(404, cb); },
function(cb) { request(app).get('/0').expect(200, cb); },
function(cb) { request(app).get('/0/edit').expect(404, cb); },
function(cb) { request(app).put('/0').send({prop1: 'new value'}).expect(404, cb); },
function(cb) { request(app).delete('/0').expect(404, cb); },
], done);
});
回答by Phil
This can be most elegantly solved with promises, and there's a really useful library to use promises with supertest: https://www.npmjs.com/package/supertest-as-promised
使用 promise 可以最优雅地解决这个问题,并且有一个非常有用的库可以将 promise 与 supertest 结合使用:https://www.npmjs.com/package/supertest-as-promised
Their example:
他们的例子:
return request(app)
.get("/user")
.expect(200)
.then(function (res) {
return request(app)
.post("/kittens")
.send({ userId: res})
.expect(201);
})
.then(function (res) {
// ...
});
回答by Tom S?derlund
I built upon Tim's replybut used async.waterfallinstead, to be able to do assert tests on the results (note: I use Tapehere instead of Mocha):
我基于Tim 的回复,但async.waterfall改为使用,以便能够对结果进行断言测试(注意:我在这里使用Tape而不是 Mocha):
test('Test the entire API', function (assert) {
const app = require('../app/app');
async.waterfall([
(cb) => request(app).get('/api/accounts').expect(200, cb),
(results, cb) => { assert.ok(results.body.length, 'Returned accounts list'); cb(null, results); },
(results, cb) => { assert.ok(results.body[0].reference, 'account #0 has reference'); cb(null, results); },
(results, cb) => request(app).get('/api/plans').expect(200, cb),
(results, cb) => request(app).get('/api/services').expect(200, cb),
(results, cb) => request(app).get('/api/users').expect(200, cb),
],
(err, results) => {
app.closeDatabase();
assert.end();
}
);
});

