我正在用 supertest 测试一个 express API。
我无法在一个测试用例中获得多个请求来使用 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();
});
);
我在这里遗漏了什么,或者是否有另一种方法可以将 http 调用与 superagent 链接起来?
最佳答案
试图将其放在上面的评论中,格式不正确。
我正在使用异步,它是真正的标准并且运行良好。
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);
});
关于node.js - 如何使用 superagent/supertest 链接 http 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21089842/