Mocha.js 当端点相互依赖时,如何在Mocha中测试API的不同端点

Mocha.js 当端点相互依赖时,如何在Mocha中测试API的不同端点,mocha.js,Mocha.js,使用Mocha测试RESTAPI。每个端点都有自己的摩卡测试。 Mocha BeforeAll清空数据库,测试填充自己的测试数据 在某个端点中,数据被添加到数据库中,然后另一个端点将其检索回来 describe('Testing endpoint /users/add', function () { it ('should successfully add data', function(done) { /// Call API endpoint

使用Mocha测试RESTAPI。每个端点都有自己的摩卡测试。 Mocha BeforeAll清空数据库,测试填充自己的测试数据

在某个端点中,数据被添加到数据库中,然后另一个端点将其检索回来

    describe('Testing endpoint /users/add', function () {
        it ('should successfully add data', function(done) {
            /// Call API endpoint with supertest to add some data
            .end(function (err, res) {
                res.status.should.be(200);
                done();
            })
        })
    });

    describe('Testing endpoint /users to get the data', function () {
        it ('should successfully retrieve the data', function (done) {
            /// Call API endpoint with supertest to retrieve data
            .end(function (err, res) {
                res.status.should.be.eql(200);
                res.body.should.have.property('users').with.lengthOf(1);
                done();
            })
        })
    });
但是,第二个descripe返回一个空数组。“添加”端点仅比“检索”端点慢

还尝试将两个“it”语句放在一个“descripe”中,但结果相同:检索时返回空数组

问题是:如何使异步测试同步运行

一般问题是:当端点相互依赖时,如何在Mocha中测试API的不同端点。 虽然代码示例似乎只显示了每个“it”部分的一个supertest调用,但实际上我在“it”部分中有多个supertest调用,最后只有一个done()。启动这些服务器调用并执行done()(不等待单个服务器调用)

如果您使用done回调,那么Mocha将等待