Using Chai

Chai is an assertion library with some tasty syntax sugar that can be paired with any other testing framework. It lets us write tests in a TDD (Test Driven Development) style or BDD (Behavior Driven Development) style. We already know what TDD is (read the intro!), so what exactly is BDD? Well BDD is the combination of using TDD with natural language constructs (English-like sentences) to express the behavior and outcomes of unit tests. Jasmine already uses a TDD style, so we'll be using Chai for its BDD interfaces, mainly through the use of should and expect.

  1. describe('Testing math', () => {
  2. it('multiplying should work', () => {
  3. let testMe = 16;
  4. // Using the expect interface
  5. chai.expect(testMe).to.be.a('number');
  6. chai.expect(testMe).to.equal(16);
  7. // Using the should interface
  8. chai.should();
  9. testMe.should.be.a('number');
  10. testMe.should.equal(16);
  11. });
  12. });

The expect and should interface both take advantage of chaining to construct English-like sentences for describing tests. Once you've decided on a style you should maintain that style for all your other tests. Each style has its own unique syntax; refer to the documentation for that specific API.

原文: https://angular-2-training-book.rangle.io/handout/testing/intro-to-tdd/using-chai.html