在我的上等咖啡测验中,我有两个describe块。在每个describe块中,我至少有两个“ it”块。第二个describe块重复第一个describe块所做的类似操作,并重复一些额外的操作。当我进行一次运行时,我觉得两个describe块同时开始,导致测试用例失败。如果我通过注释掉一个describe块单独运行它们,则它们运行良好。

请注意,每次我清除数据库并从头开始运行时,我所测试的每个API都是自包含的,而不依赖于其他describe块的结果。

如何对它们进行排序,以使第二个描述块在第一个描述块之后运行,依此类推,如果我有两个以上的描述块。

下面是我的代码:

var server;
var mongodb;

before(function (done) {
    server = require('../../../app.js'); // same as "node app.js"
    done();
})

after(function (done) {
    server.close();
})

beforeEach(function (done){
    clear_the_db();
    done();
})

var json_obj = {"a":"b"};
function clear_the_db() {
    var mongoObj = mongoose.model('modelname');
    mongoObj.remove({}, function(err){
    if(!err) {
        console.log('MongoDb collections removed');
    } else {
        console.log('Error is= '+err);
    }
})

describe('First:POST call to insert data into project', ()=> {
    clear_the_db();
    it('First:Creating project', (done) => {
        chai.request(server)
        .post('/create/myproject')
        .send()
        .end((err, res) => {
            expect(res.statusCode).to.equal(200);
            done();
        });
    });

    it('First:Inserting data into created project', (done) => {
        chai.request(server)
        .post('/data/myproject')
        .send(json_obj)
        .end((err, res) => {
            expect(res.statusCode).to.equal(200);
            done();
        });
    });

});
describe('Second:POST call to insert data into project', ()=> {
    clear_the_db();
    it('Second:Creating project', (done) => {
        chai.request(server)
        .post('/create/myproject')
        .send()
        .end((err, res) => {
            expect(res.statusCode).to.equal(200);
            done();
        });
    });

    it('Second:Inserting data into created project', (done) => {
        chai.request(server)
        .post('/data/myproject')
        .send(json_obj)
        .end((err, res) => {
            expect(res.statusCode).to.equal(200);
            done();
        });
    });

    it('Second:Fetching data from the created project', (done) => {
        chai.request(server)
        .get('/data/myproject')
        .end((err, res) => {
            expect(res.statusCode).to.equal(200);
            done();
        });
    });
});


https://mochajs.org/读取钩子后更新了代码:

var server;
var mongodb;

before(function (done) {
    server = require('../../../app.js'); // same as "node app.js"
    done();
})

after(function (done) {
    server.close();
})


var json_obj = {"a":"b"};
function clear_the_db() {
    var mongoObj = mongoose.model('modelname');
    mongoObj.remove({}, function(err){
        if(!err) {
            console.log('MongoDb collections removed');
        } else {
            console.log('Error is= '+err);
        }
    })
}

describe("This is outer-most describe", function() {
    beforeEach(function (done){
        clear_the_db();
    })

    describe('First:POST call to insert data into project', ()=> {
        it('First:Creating project', (done) => {
            chai.request(server)
            .post('/create/myproject')
            .send()
            .end((err, res) => {
                 expect(res.statusCode).to.equal(200);
                 chai.request(server)
                .post('/data/myproject')
                .send(json_obj)
                .end((err, res) => {
                    expect(res.statusCode).to.equal(200);
                });
                done();
            });
        });
    });

    describe('Second:POST call to insert data into project', ()=> {
        it('Second:Creating project', (done) => {
            chai.request(server)
            .post('/create/myproject')
            .send()
            .end((err, res) => {
                expect(res.statusCode).to.equal(200);
                chai.request(server)
                .post('/data/myproject')
                .send(json_obj)
                .end((err, res) => {
                     expect(res.statusCode).to.equal(200);
                     chai.request(server)
                     .get('/data/myproject')
                     .end((err, res) => {
                         expect(res.statusCode).to.equal(200);
                     });
                });
                done();
            });
        });
    });
});

最佳答案

默认情况下,Mocha测试按顺序运行。您必须在其他地方寻找问题。

可能的原因:


您的两个套件都具有相同的名称-POST call to insert data into project
如果clear_the_db();是异步的,则不能保证在运行it()块之前已完成执行。相反,您应该在beforeEach挂钩回调中进行清理,并在任务完成后继续进行下一个测试案例。例:

function clear_the_db(doneCb) {
  var mongoObj = mongoose.model('modelname');

  mongoObj.remove({}, function(err){
    if(!err) {
      console.log('MongoDb collections removed');
    } else {
      console.log('Error is= '+err);
    }
    // call the Mocha done() callback function
    doneCb();
  });
}

// in your beforeEach hook, pass 'done' to 'clear_the_db'
beforeEach(function (done){
  clear_the_db(done);
});

09-25 16:08