Angular jasmine单元测试框架里使用it函数定义single spec

151 阅读1分钟

it函数接收两个参数,描述信息和包含了待测试的单元测试代码的函数:


    /**
     * Define a single spec. A spec should contain one or more {@link expect|expectations} that test the state of the code.
     *
     * A spec whose expectations all succeed will be passing and a spec with any failures will fail.
     * The name `it` is a pronoun for the test target, not an abbreviation of anything. It makes the
     * spec more readable by connecting the function name `it` and the argument `description` as a
     * complete sentence.
     * @name it
     * @since 1.3.0
     * @function
     * @global
     * @param {String} description Textual description of what this spec is checking
     * @param {implementationCallback} [testFunction] Function that contains the code of your test. If not provided the test will be `pending`.
     * @param {Int} [timeout={@link jasmine.DEFAULT_TIMEOUT_INTERVAL}] Custom timeout for an async spec.
     * @see async
     */
    it: function() {
      return env.it.apply(env, arguments);
    },

这里能看到其实还有第三个可选参数,timeout即超时时间。

jasmineEnv变量是个巨大的对象,包含了很多jasmine测试方法的实现:

这个it方法里,也包含了将待测试代码通过wrapTestInZone放入Zone里执行:

it函数调用SpecFactory创建一个spec实例出来,然后返回:


注意,此时只是将it函数传入的待测试代码即箭头函数,包裹成jasmine test spec,但还未真正开始执行。

箭头函数真正的执行还是在Zone里:

更多Jerry的原创文章,尽在:“汪子熙”: