node.js - chai 如何使用期望显示测试失败?
问题描述
chai 中的断言失败不会将测试报告为失败。
我尝试过使用断言而不是期望。我尝试通过缺少预期值中的字符来导致测试失败。
const axios = require('axios');
var assert = require('assert');
var expect = require('chai').expect;
describe('Tests', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.equal([1, 2, 3].indexOf(4), -1);
});
it('should return 1 when index is 2', function () {
assert.equal([1, 2, 3].indexOf(3), 2)
});
});
describe('#http-get', function () {
it('should be https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jpg', function () {
axios.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY')
.then(response => {
// assert.equal(response.data.url, 'https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jpg');
expect(response.data.url).to.equal('https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jp');
})
.catch(error => {
console.log(error);
});
});
});
});
我预计输出状态为 2 通过和 1 失败,但我看到以下输出,其中第三个断言标记为通过,但打印了断言失败。
Tests
#indexOf()
✓ should return -1 when the value is not present
✓ should return 1 when index is 2
#http-get
✓ should be https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jpg
3 passing (34ms)
{ AssertionError: expected 'https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jpg' to equal 'https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jp'
at axios.get.then.response (/Users/adityai/nodejs-workspace/axios-sample/test/axios-sample-test.js:20:50)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
message: 'expected \'https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jpg\' to equal \'https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jp\'',
showDiff: true,
actual: 'https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jpg',
expected: 'https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jp' }
解决方案
无论您使用expect
or assert
,当断言失败时,chai 都会抛出错误。您不应该处理该错误,因为 Mocha 依赖于错误来确定测试用例是否应该失败。
此外,如果您的测试用例是异步的,请记住done
在异步任务完成时返回 Promise 或调用回调。
describe('#http-get', function () {
it('should be https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jpg', function () {
return axios.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY')
.then(response => {
expect(response.data.url).to.equal('https://apod.nasa.gov/apod/image/1905/volcano_stefnisson_960.jp')
})
// .catch(error => {
// console.log(error);
// })
})
})
推荐阅读
- analytics - Power BI Desktop 导入数据限制
- google-cloud-platform - 如何仅将目录下许多子目录中的文件复制到 GCP 中的另一个项目存储桶中?
- c# - 使用 .Nest() 或 .Read() 时出现 SQLDataReader 错误
- java - 警告:在“OnLifecycleEvent”类型中找不到注释方法“value()”:找不到 android.arch.lifecycle.OnLifecycleEvent 的类文件
- git - GIT存储库的所有文件同时标记为“丢失”和“新文件” [SourceTree]
- polymer-3.x - shady-render.ts:46 Uncaught TypeError: result.getTemplateElement is not a function
- javascript - 由于语法无效 (ASP.NET),服务器无法处理 PUT 请求
- docker - Gitlab-CI 部署阶段和任务因错误的 Rancher API Url 和密钥而失败
- python - ValueError:检查输入时出错:预期 conv1d_1_input 有 3 个维度,但得到了形状为 (3856, 52) 的数组
- ruby-on-rails - 打开从 Angular 上传到 Rails 应用程序的图像时出错