javascript - 理解 Chai 的断言(期望式)
问题描述
这里有什么区别?
it("should be 5", () => {
expect(num).equal(5);
});
it("should be 5", () => {
expect(num).to.be.equal(5);
});
如果我使用第一种方式或第二种方式,它没有任何区别。至少在我看来是这样。
“.to.be.”的目的是什么?
仅仅是为了拥有更类似于实际句子的东西吗?或者它有什么功能?
解决方案
从文档
以下内容作为可链接的 getter 提供,以提高断言的可读性。
看这个测试用例:
import { expect } from 'chai';
const num = 5;
describe('62770625', () => {
it('should be 5', () => {
expect(num).equal(5);
});
it('should be 5', () => {
expect(num).to.be.equal(5);
});
it('should have members', () => {
expect([{ a: 1 }]).deep.members([{ a: 1 }]);
expect([{ a: 1 }]).to.have.deep.members([{ a: 1 }]);
});
});
它们都可以工作,但是通过这些语言链,我们可以得到:
- 更好的可读性
- 人性化
- 良好的语义
推荐阅读
- google-apps-script - 使用应用程序脚本在谷歌表格中透视或转置数据
- python - 在 Python input() 函数中插入分隔符
- linux - 我想安装“邮件”但是
- drupal - Drupal 7. 如何在主内容字段中添加一段可重用的html(或块)
- python - Python:循环内矩阵每个元素的绝对值
- javascript - [Vue 警告]:未定义属性或方法“$v”
- ios - 加入混合的从右到左和从左到右的语言字符串时的奇怪行为
- php - 提交 ajax 表单不适用于 PHP $_FILES
- azure - 使用逻辑应用从 Azure 服务总线主题读取和删除一条或多条消息
- python - 基于有限价值的每日回报率 - 熊猫