注册
登录
html/HTML5
Mocha Chai 基本 GET 请求未记录通过并正确失败
返回
Mocha Chai 基本 GET 请求未记录通过并正确失败
作者:
糖果
发布时间:
2024-03-14 06:03:12 (14天前)
我对 mocha.js 很陌生,所以试图用一个最小的例子来获得一些相当基本和可靠的东西。 我正在尝试对示例 JSON API 发出 GET 请求并对其运行 3 个测试,所有测试都应该有不同的结果,但都得到了与预期不同的结果。 此外,我正在尝试捕获和计数/报告通过测试发生的错误,以输出after()所有测试运行的详细信息,以免弄乱 UI。 对于 3 个测试,我只有 1 个应该通过。问题是,如果我包含一个.finally()调用,那么所有调用都会通过,如果我删除.finally()调用并且只有.catch()调用,那么它们都失败了。不知道我错过了什么。 let errors = [] // After All output error details after((done) => { console.log("Total Errors: " + errors.length) // console.log(errors) done() }) // Example Test Suite describe("Example Test Suite", () => { it("#1 Should Pass on expect 200", (done) => { request("https://jsonplaceholder.typicode.com/") .get("todos/1") .expect(200) .catch(function (err) { errors.push(err) done(err) }) .finally(done()) }) it("#2 Should Fail on wrong content type", (done) => { request("https://jsonplaceholder.typicode.com/") .get("todos/1") .expect("Content-Type", "FAIL") .catch(function (err) { errors.push(err) done(err) }) .finally(done()) }) it("#3 Should Fail on contain.string()", (done) => { request("https://jsonplaceholder.typicode.com/") .get("todos/1") .then(function (err, res) { if (err) {done(err)} else { try { expect(res.text).to.contain.string("ThisShouldFail"); } catch(e) { errors.push({error: e, response: res}) done(err) } finally { done() } } }).catch(function (err) { errors.push(err) done(err) }) .finally(done()) }) }) let errors = [] // After All output error details after((done) => { console.log("Total Errors: " + errors.length) // console.log(errors) done() }) // Example Test Suite describe("Example Test Suite", () => { it("#1 Should Pass on expect 200", (done) => { request("https://jsonplaceholder.typicode.com/") .get("todos/1") .expect(200) .catch(function (err) { errors.push(err) done(err) }) .finally(done()) }) it("#2 Should Fail on wrong content type", (done) => { request("https://jsonplaceholder.typicode.com/") .get("todos/1") .expect("Content-Type", "FAIL") .catch(function (err) { errors.push(err) done(err) }) .finally(done()) }) it("#3 Should Fail on contain.string()", (done) => { request("https://jsonplaceholder.typicode.com/") .get("todos/1") .then(function (err, res) { if (err) {done(err)} else { try { expect(res.text).to.contain.string("ThisShouldFail"); } catch(e) { errors.push({error: e, response: res}) done(err) } finally { done() } } }).catch(function (err) { errors.push(err) done(err) }) .finally(done()) }) }) let errors = [] // After All output error details after((done) => { console.log("Total Errors: " + errors.length) // console.log(errors) done() }) // Example Test Suite describe("Example Test Suite", () => { it("#1 Should Pass on expect 200", (done) => { request("https://jsonplaceholder.typicode.com/") .get("todos/1") .expect(200) .catch(function (err) { errors.push(err) done(err) }) .finally(done()) }) it("#2 Should Fail on wrong content type", (done) => { request("https://jsonplaceholder.typicode.com/") .get("todos/1") .expect("Content-Type", "FAIL") .catch(function (err) { errors.push(err) done(err) }) .finally(done()) }) it("#3 Should Fail on contain.string()", (done) => { request("https://jsonplaceholder.typicode.com/") .get("todos/1") .then(function (err, res) { if (err) {done(err)} else { try { expect(res.text).to.contain.string("ThisShouldFail"); } catch(e) { errors.push({error: e, response: res}) done(err) } finally { done() } } }).catch(function (err) { errors.push(err) done(err) }) .finally(done()) }) }) let errors = [] // After All output error details after((done) => { console.log("Total Errors: " + errors.length) // console.log(errors) done() }) // Example Test Suite describe("Example Test Suite", () => { it("#1 Should Pass on expect 200", (done) => { request("https://jsonplaceholder.typicode.com/") .get("todos/1") .expect(200) .catch(function (err) { errors.push(err) done(err) }) .finally(done()) }) it("#2 Should Fail on wrong content type", (done) => { request("https://jsonplaceholder.typicode.com/") .get("todos/1") .expect("Content-Type", "FAIL") .catch(function (err) { errors.push(err) done(err) }) .finally(done()) }) it("#3 Should Fail on contain.string()", (done) => { request("https://jsonplaceholder.typicode.com/") .get("todos/1") .then(function (err, res) { if (err) {done(err)} else { try { expect(res.text).to.contain.string("ThisShouldFail"); } catch(e) { errors.push({error: e, response: res}) done(err) } finally { done() } } }).catch(function (err) { errors.push(err) done(err) }) .finally(done()) }) })
收藏
举报
2 条回复
1#
回复此人
糖果
|
2021-08-12 14-07
我发现提供的测试存在两个问题 .finally(done())立即成功完成测试,因为.finally期待回调并被done原地调用,.finally(done)或者.finally(() => done())可能按您的预期工作。 通过 try/catch 拦截错误是一种非常糟糕的做法,您的实现将在将测试标记为通过时吞下潜在的错误,即使它失败了,最好使用 mocha 的测试上下文,因为这是一个 afterEach 钩子,因为例子: const errors = []; afterEach(function () { if (this.currentTest.state === "failed") { errors.push(this.currentTest.title) } }); 在 mocha 中处理 promise 时也不需要调用 done。一个promise可以简单地返回,并且当promise解决时mocha将通过测试,否则失败(如果错误未被捕获)。 it("...", () => { return request(...).get(...).then(...) } 或使用 async/await,它隐式返回一个承诺 it("...", async () => { const result = await request(...).get(...) // Do assertions on result ... // No need to return anything here }
编辑
登录
后才能参与评论