柴如何期待功能的运作? [英] How does the chai expect function work?

查看:111
本文介绍了柴如何期待功能的运作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从chai的api你有这样的代码:

From chai's api you've got code like this:

.exist

Asserts that the target is neither null nor undefined.

var foo = 'hi'
  , bar = null
  , baz;

expect(foo).to.exist;
expect(bar).to.not.exist;
expect(baz).to.not.exist;

这是如何存在的? expect函数返回一个对象,然后在to对象上只有一个属性查找。这只是一个房产评估,不是吗?对我来说唯一有意义的是,如果存在属性是一个getter方法。

How does that exist part work? The expect function returns an object, then there's simply a property lookup on the "to" object. That's simply a property evaluation though isn't it? The only thing that would make sense to me is if the exist property is a getter method.

去了什么?

推荐答案

chai公开使用方法来访问 chai 导出和它是 utils

chai exposes an use method to access the chai export and it's utils.

创建插件,但它也在内部用于加载它的界面。

This method can be used by third parties when creating plugins, but it's also used internally to load it's interface.

这个的实现方法很简单:

The implementation of this method is simple:

exports.use = function (fn) {
  if (!~used.indexOf(fn)) {
    fn(this, util);
    used.push(fn);
  }

  return this;
};

在内部,它使用它来加载(以及其他)主 Assertion原型和核心断言功能:

Internally it uses this to load (among other) the primary Assertion prototype and the core assertion functionality:

var assertion = require('./chai/assertion'); // primary Assertion prototype
exports.use(assertion); // load it

var core = require('./chai/core/assertions'); // core assertion functionality
exports.use(core); // load it

Assertion原型公开的方法之一 addProperty 方法,它允许您向所述原型添加属性。

One of the methods that are exposed by the Assertion prototype is the addProperty method which allows you to add properties to said prototype.

内部 chai 使用此方法将核心断言功能添加到断言原型。例如,以这种方式添加所有语言链和断言助手(存在等)。

Internally chai uses this method to add the core assertion functionality to the Assertion prototype. For instance, all language chains and assertion helpers (exist, empty, etc) are added this way.

语言链:

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

当内部加载特定接口时,所有这些功能都可用,例如预期。加载此接口后,只要执行 expect ,就会实例化一个新的 Assertion原型,其中包含所有功能:

All this functionality becomes available when a specific interface gets loaded internally, for instance expect. When this interface is loaded, a new Assertion prototype will be instantiated whenever expect gets executed, which will contain all functionality:

// load expect interface
var expect = require('./chai/interface/expect'); // expect interface
exports.use(expect); // load it

// expect interface
module.exports = function (chai, util) {
  chai.expect = function (val, message) {
    return new chai.Assertion(val, message); // return new Assertion Object with all functionality
  };
};

正如您所见, expect 方法接受一个 val 参数(以及一个可选的消息参数)。调用此方法时(例如 expect(foo))将实例化并返回一个新的断言原型,从而暴露出来所有核心功能(允许你做 expect(foo).to.exist )。

As you can see the expect method accepts a val argument (and an optional message argument). When this method is called (for instance expect(foo)) a new Assertion prototype will be instantiated and returned, exposing all core functionality (allowing you to do expect(foo).to.exist).

Assertion Constructor 使用标志 util 在Object上设置标志值映射到传入的 val 参数。

The Assertion Constructor uses the flag util to set a flag value on the Object that maps to the passed in val argument.

  function Assertion (obj, msg, stack) {
    flag(this, 'ssfi', stack || arguments.callee);
    flag(this, 'object', obj); // the 'object' flag maps to the passed in val
    flag(this, 'message', msg);
  }

所有存在然后是,通过标志 util 获取此值,并评估它是否等于 null ,使用 Assertion原型上定义的断言方法。

All exist then does, is get this value through the flag util and evaluates if it not equals to null, using the assert method defined on the Assertion prototype.

  Assertion.addProperty('exist', function () {
    this.assert(
        null != flag(this, 'object')
      , 'expected #{this} to exist'
      , 'expected #{this} to not exist'
    );
  });

这篇关于柴如何期待功能的运作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆