尽管未实现 URL 处理程序,但 Mocha 测试正常

The Mocha test is ok despite the URL handler not being implemented

我创建了 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"

有人知道这个吗?

因为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