zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Angular Jasmine 里一些常用概念学习笔记 - describe, it, beforeEach的用法

ITAngular笔记 常用 用法 一些 Jasmine
2023-09-14 09:04:01 时间

describe: 定义一个test spec group,用来包裹多个specs,也称为suite:

describe的第二个参数是一个函数,待测试的spec,即测试逻辑,就写在该函数里。

/**
     * Create a group of specs (often called a suite).
     *
     * Calls to `describe` can be nested within other calls to compose your suite as a tree.
     * @name describe
     * @since 1.3.0
     * @function
     * @global
     * @param {String} description Textual description of the group
     * @param {Function} specDefinitions Function for Jasmine to invoke that will define inner suites and specs
     */

先执行describe,再执行beforeEach:

然后执行describe内层嵌套的describe:

这里能看出,describe定义的单个spec,其函数会立即执行:

it函数定义了一个test spec,里面必须包含expectation等测试元素。这里的it不是某个概念的缩写,而只是一个代词。

然后执行beforeEach,这是一个异步调用。从调用栈很难能够分辨出这个beforeEach具体是被谁调用的:

StoreModule.forRoot返回一个组装好的NgModule结构:

StoreModule.forFeature: 接受的输入参数为字符串feature和其他module export的getReducers:

store实例通过TestBed.inject注入好之后,在其私有字段_value里,立即能看到里面处于initial状态的state:

此时从依赖注入器injector里可以直接拿到record了,奇怪:

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