node.js - 如何使用 superagent/supertest 链接 http 调用?

标签 node.js express supertest superagent

我正在用 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/

相关文章:

javascript - 开发中的 Angular 5 通用 : Using server. ts

file-upload - 如何在 sails 中指定 Express 的文件上传目录?

node.js - Azure NodeJS WebApp 上的静态服务 Socket.IO

node.js - 通过 Grunt 运行 Node 应用程序

node.js - 快速重定向和本地

javascript - Sinon stub 无法与 module.exports = { f1, f2} 一起使用

node.js - Supertest/Jest 检查 header 是否存在

node.js - NPM Run Build 始终构建生产而不是开发

node.js - updateMany()之后如何获取所有更新文档的值?