【问题标题】:what is the difference between .done() and .end() function in node and when they shall be used?node 中的 .done() 和 .end() 函数有什么区别,何时使用?
【发布时间】:2020-06-02 11:44:17
【问题描述】:

我在用 mocha 和 Chai 编写 API 测试脚本时试图了解 end() 函数的用例。同时,我很困惑我是否应该在这里使用done()函数,以及.end().done()之间的确切区别。

代码如下:

describe("Suite", () => {
    it('Post Test case', (done) => {
        request('https://reqres.in')
        .post('/api/users')
        .send({
            "name": "morpheus",
            "job": "leader"
        })
        .set('Accept', 'application/json')
        .expect(200,'Content-Type', /json/)
        .then((err,res) => {
            console.log(JSON.stringify(err))
            console.log(JSON.stringify(res.body))
            console.log(JSON.stringify("           "))

        })
        done();
    });
    it('Put Test case', (done) => {
        request('https://reqres.in')
        .put('/api/users/2')
        .send({
            "name": "morpheus",
            "job": "zion residents"
        })
        .expect(200)
        .end((err, res) => {
            console.log(JSON.stringify(err))
            console.log(JSON.stringify(res.body))
            console.log(JSON.stringify("           "))

        })
        done();
    })
})

【问题讨论】:

    标签: node.js api callback mocha.js supertest


    【解决方案1】:

    你把东西混在一起了。

    endexpressjs framework 的一个方法,它结束服务器响应。

    donemocha test function 的参数。您在完成 异步 测试后调用此参数,让 mocha 知道您的异步代码已执行完毕,它可以继续进行另一个测试。

    在你的情况下,你需要他们两个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 2014-08-28
      • 2018-07-08
      • 2013-12-02
      • 2016-12-27
      相关资源
      最近更新 更多