expect().to.be.true 在 Chai 中如何工作?

How does the expect().to.be.true work in Chai?

expect(true).to.be.true;

在此代码中,所有'to'、'be'、'true'似乎都是来自'expect(true)'的对象响应的属性。

这些属性如何工作才能引发异常?

查看 chai Assertion 的来源,但 tl;dr 是 Chai 在其断言库上实现了自己的可链接方法。但是,特殊关键字只是语法糖,如下面的代码所示。从字面上看,它们只是添加并可以链接的属性,但没有真正定义:

  /**
   * ### Language Chains
   *
   * The following are provided as chainable getters to improve the readability
   * of your assertions.
   *
   * **Chains**
   *
   * - to
   * - be
   * - been
   * - is
   * - that
   * - which
   * - and
   * - has
   * - have
   * - with
   * - at
   * - of
   * - same
   * - but
   * - does
   *
   * @name language chains
   * @namespace BDD
   * @api public
   */

  [ 'to', 'be', 'been'
  , 'is', 'and', 'has', 'have'
  , 'with', 'that', 'which', 'at'
  , 'of', 'same', 'but', 'does' ].forEach(function (chain) {
    Assertion.addProperty(chain);
  });

从那里它实际寻找的是它专门定义的关键字。所以一个例子是关键字 .to.be.true 它将在下面的代码片段中查看 true as defined

  /**
   * ### .true
   *
   * Asserts that the target is strictly (`===`) equal to `true`.
   *
   *     expect(true).to.be.true;
   *
   * Add `.not` earlier in the chain to negate `.true`. However, it's often best
   * to assert that the target is equal to its expected value, rather than not
   * equal to `true`.
   *
   *     expect(false).to.be.false; // Recommended
   *     expect(false).to.not.be.true; // Not recommended
   *
   *     expect(1).to.equal(1); // Recommended
   *     expect(1).to.not.be.true; // Not recommended
   *
   * A custom error message can be given as the second argument to `expect`.
   *
   *     expect(false, 'nooo why fail??').to.be.true;
   *
   * @name true
   * @namespace BDD
   * @api public
   */

  Assertion.addProperty('true', function () {
    this.assert(
        true === flag(this, 'object')
      , 'expected #{this} to be true'
      , 'expected #{this} to be false'
      , flag(this, 'negate') ? false : true
    );
  });

可以查看源码:

[ 'to', 'be', 'been'
  , 'is', 'and', 'has', 'have'
  , 'with', 'that', 'which', 'at'
  , 'of', 'same', 'but', 'does' ].forEach(function (chain) {
    Assertion.addProperty(chain);
});

并且 utils 中有一个 addProperty:
https://github.com/chaijs/chai/blob/master/lib/chai/utils/addProperty.js

有了这个,您可以无限地链接属性,例如:.to.be.to.to.to.be.equal()

让我们创建一个更简单的演示:

假设您有一个 assert 对象,使用 .true() 方法

const assert = {
  'true': function (v) {
    return !!v
  }
}

并且您希望能够无限地链接 .to。只需使用 defineProperty 来定义我们的 getter:

Object.defineProperty(assert, 'to', {
  get() {
    return assert
  }
})

所以现在你可以

assert.to.to.to.to.true(false)

工作代码:https://codepen.io/CodinCat/pen/LLzBEX?editors=0012


我在这里添加了另一个更复杂的示例:https://codepen.io/CodinCat/pen/dRVjXL?editors=0012

在此示例中,您可以看到 .true 属性.

中存在一些行为

我们把expect()的值存入内部__expectObj__value属性,然后在getter的[=]中验证22=]。所以你可以

expect(false).to.to.to.to.true