如何在Mocha和Chai中处理异步测试?

时间:2019-09-05 07:29:25

标签: javascript mocha

我需要按顺序运行一系列测试。所有测试都是异步的,并取决于上一个。我在处理测试的异步方面遇到很多麻烦。

我尝试使用摩卡步,但没有用。

step is not a function

我还尝试将一个describe块嵌套在另一个describe块内的before()钩子中。在这种情况下,我将进行猫鼬查询并分配在另一个未发生分配的变量中接收到的数据。我以为before()块应该可以解决这个问题?

var token
describe("Email confirmation", function () {

        before(function () {
            describe("Signup test", function () {
                it("Should register user and sends an email confiramtion token to registred email Id", function (done) {
                    agent
                        .post('/register')
                        .send({
                            unique_username: "xyz",
                            email: "xyz@gmail.com",
                            password: "12345"

                        })
                        .end(function (err, res) {
                            res.should.have.property('status', 200);
                            expect(res.body).to.contain.property('type')
                            expect(res.body).to.contain.property('msg')
                            // expect(res.body).to.deep.equal({});

                            done();
                        })
                })
            })

            User.findOne({ email: "xyz@gmail.com" }, function (err, data) {

                Token.findOne({ _userId: data._id }, function (err, data1) {

                    token = data1.token

                })

            })
        })


        it("Should verify the email Id of a registered user", function (done) {

            agent
                .post('/email_confirmation')
                .send({
                    email: "xyz@gmail.com.com",
                    token: token
                })
                .end(function (err, res) {
                    res.should.have.property('status', 200);
                    expect(res.body).to.contain.property('type')
                    expect(res.body).to.contain.property('msg')

                    done();
                })
        }).timeout(10000)
    })

期望作为“ Signup测试”块的before()首先执行,然后再执行另一个describe块。

0 个答案:

没有答案