【问题标题】:The Mocha test is ok despite the URL handler not being implemented尽管 URL 处理程序未实现,但 Mocha 测试正常
【发布时间】:2019-03-23 11:10:54
【问题描述】:

我创建了 firebase 函数,我想在本地使用 Google Cloud Function Emulator 和 Mocha 对其进行测试。

所以我在 Mocha 中创建了一个测试,使用 PUT 方法测试 REST API 更新记录功能。

测试是这样进行的

it("should succesfully update data",()=>{
    chai.request(api)
        .put(`/clients/${someId}`)
        .set('Authorization', sometoken)
        .send(somenewdata)
        .end((error,response)=>{
            expect(response.status, "should be 200").to.equal(200);
})

当我运行测试时。其实没问题。

问题是我没有实现对“clients/:id”URL 的 PUT 方法请求的处理程序。所以显然结果应该是超时。我也尝试运行模拟器,并使用 POSTMAN 发送 PUT 请求,我得到了预期的超时结果。

其他细节:

"@types/mocha": "^5.2.5"

有人知道吗?

【问题讨论】:

    标签: node.js typescript mocha.js google-cloud-functions


    【解决方案1】:

    这是因为chai.request 是异步函数,所以必须在测试完成时告诉 Mocha。解决方法是我们可以使用done

    it("should succesfully update data",(done) => { // specify done
        chai.request(api)
            .put(`/clients/${someId}`)
            .set('Authorization', sometoken)
            .send(somenewdata)
            .end((error,response)=>{
               expect(response.status, "should be 200").to.equal(200);
               done(); // add done
            })
    })
    

    参考: https://mochajs.org/#asynchronous-code

    【讨论】:

      猜你喜欢
      • 2021-05-18
      • 2013-12-13
      • 2018-09-03
      • 2019-08-18
      • 2021-11-03
      • 2013-10-07
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      相关资源
      最近更新 更多