【问题标题】: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 }]);
  });
});

它们都可以工作,但是通过这些语言链,我们可以得到:

  • 更好的可读性
  • 人性化
  • 良好的语义

【讨论】:

    猜你喜欢
    • 2017-03-04
    • 2022-01-23
    • 2017-10-02
    • 2016-05-27
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    相关资源
    最近更新 更多