Как вставить задержку между двумя блоками блока описания в тесте Мокко-Чай? - PullRequest
1 голос
/ 23 апреля 2019

Я хочу вставить задержку между двумя it () моего блока описаний.Вторая функция it () будет извлекать данные, которые были переданы между периодами времени.Перед выполнением первого метода it () я удерживаю переменную time in time1, затем с помощью функции setTimeout, указанной ниже, выполняю функцию it (), отправляя time1 и time2 (время окончания).

Однако, похоже, что вторая функция it () не работает согласно моему требованию.Как мне нужно изменить его или как вызвать второй it () с задержкой?

        var chai = require('chai');
        var chaiHttp = require('chai-http');    
        var should = chai.should();
        var expect = chai.expect;
        var http = require('http');
        chai.use(chaiHttp);
        var server;
        var mongodb;

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

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

        describe('POST call to insert data into project', ()=> {
            var time1= new Date();
            time1 = time1.getTime();
            it('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();
                });
            });  //it    


            /* Below it() block should be executed after 30s with the time1 and 
               time2 variable */
            it("Doing total counting", (done) => {

             // this.timeout(30000);
             setTimeout(function () {
               var time2= new Date();
               time2 = time2.getTime();
               var url  = 'total?start_time=' + time1 + '&end_time=' + time2;
            chai.request(server)
                .get(url)
                .send()
                .end((err, res) => {
                    expect(res.statusCode).to.equal(200);

                    done();
                }
                )
        }, 30000)

    })     
});// describe

1 Ответ

1 голос
/ 23 апреля 2019

Похоже, вы могли бы извлечь выгоду из встроенной this.timeout() функциональности

        this.timeout(30000);

        it("Doing total counting", (done) => {             
           var time2= new Date();
           time2 = time2.getTime();
           var url  = 'total?start_time=' + time1 + '&end_time=' + time2;
        chai.request(server)
        ...    
        setTimeout(3000, done)
...