【问题标题】:Understanding Chai's assertions (expect-style)理解 Chai 的断言(期望式)
【发布时间】:2020-07-07 07:59:42
【问题描述】:
这里有什么不同?
it("should be 5", () => {
expect(num).equal(5);
});
it("should be 5", () => {
expect(num).to.be.equal(5);
});
我使用第一种方式或第二种方式没有任何区别。至少在我看来是这样。
“.to.be.”的目的是什么?
仅仅是因为有一些更类似于实际句子的东西吗?或者它有什么功能?
【问题讨论】:
标签:
javascript
unit-testing
automated-tests
mocha.js
chai
【解决方案1】:
来自doc
以下内容作为可链接的 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 }]);
});
});
它们都可以工作,但是通过这些语言链,我们可以得到: