How to chain http calls with superagent/supertest?

后端 未结 4 1836
一个人的身影
一个人的身影 2021-02-01 13:28

I am testing an express API with supertest.

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 c

4条回答
  •  灰色年华
    2021-02-01 13:46

    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();
                    });
            });
        });
    });
    

提交回复
热议问题