Angular jasmine单元测试框架里describe的实现原理
源代码:describe函数传入的两个参数:描述信息和箭头函数:
从注释能看出,describe函数的语义:Create a group of specs (often called a suite)
getJasmineRequireObj().interface = function(jasmine, env) {var jasmineInterface = {/*** Callback passed to parts of the Jasmine base interface.** By default Jasmine assumes this function completes synchronously.* If you have code that you need to test asynchronously, you can declare that you receive a `done` callback, return a Promise, or use the `async` keyword if it is supported in your environment.* @callback implementationCallback* @param {Function} [done] Used to specify to Jasmine that this callback is asynchronous and Jasmine should wait until it has been called before moving on.* @returns {} Optionally return a Promise instead of using `done` to cause Jasmine to wait for completion.*//*** 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: function(description, specDefinitions) {return env.describe(description, specDefinitions);},
参数定义
- description:Textual description of the group
- specDefinitions: Function for Jasmine to invoke that will define inner suites and specs
从这里能看出,单元测试代码运行于一个特定的zone里:
// Monkey patch all of the jasmine DSL so that each function runs in appropriate zone.var jasmineEnv = jasmine.getEnv();['describe', 'xdescribe', 'fdescribe'].forEach(function (methodName) {var originalJasmineFn = jasmineEnv[methodName];jasmineEnv[methodName] = function (description, specDefinitions) {return originalJasmineFn.call(this, description, wrapDescribeInZone(specDefinitions));};});
addSpecsToSuite:
添加Specs到Suite之后,执行:
/*** Gets a function wrapping the body of a Jasmine `describe` block to execute in a* synchronous-only zone.*/function wrapDescribeInZone(describeBody) {return function () {return syncZone.run(describeBody, this, arguments);};}
在一个同步zone里执行spec的body,即传入describe方法的箭头函数:
callback就是应用程序单元测试代码里定义的箭头函数:
即如下图所示:
更多Jerry的原创文章,尽在:“汪子熙”:
Angular jasmine单元测试框架里describe的实现原理相关推荐
- Angular jasmine单元测试框架里spyOn的创建原理
准备针对handler的handleError方法创建spy: 保存原始方法的信息到变量originalMetho里: 利用spyFactory.createSpy创建spied版本的新方法: wra ...
- Angular jasmine单元测试框架TestBed.createComponent的实现原理
单元测试代码里的createComponent,是通过TestBedRender实现的: TestBedRenderer3: 获得注入的TestComponentRenderer: const res ...
- Angular jasmine单元测试框架TestBed.inject的执行原理
单步调试这段代码: TestBed用于单元测试代码里创建Component和service实例. injectionToken即传入TestBed.inject的function: 还是delegat ...
- Angular jasmine单元测试框架里使用it函数定义single spec
it函数接收两个参数,描述信息和包含了待测试的单元测试代码的函数: /*** Define a single spec. A spec should contain one or more {@lin ...
- Angular jasmine单元测试框架里expect.toHaveBeenCalled的工作原理
第120行给handler.handleError方法注入spy后,第121行代码flush HTTP Mock request会触发handler.handleError(的wrap实现)调用.第1 ...
- Angular jasmine单元测试框架fixture.detectChanges的实现原理
源代码: fixture的类型是component-fixture,里面会调用_tick方法: 具体执行的detectChange逻辑,取决于Change Detect reference: Root ...
- Angular jasmine单元测试框架spec的运行时数据结构
以我单元测试里这段代码为例: 通过describe函数创建一个suite,即spec的集合: 真正的spec由函数it创建:其中expectable是human readable的字符串描述信息,描述 ...
- Angular jasmine单元测试框架spied method的调用记录数据结构
基于UnknownErrorHandler的handleError方法创建一个spy 方法: 第40行调用的是spied之后的新方法: 因为调用的是Spied之后的wrapper方法,在wrapper ...
- Angular单元测试框架里API toHaveBeenCalledTimes的工作原理
看这样一段代码: let spiedFirstFocusable = spyOn(keyboardFocusService,'findFirstFocusable').and.returnValue( ...
最新文章
- vs2008【断点无效】解决方法
- HTML5 开发APP
- JavaScript调用其他函数中的变量
- Codeforces Round #700 (Div. 1Div. 2)
- 四则运算计算器c语言switch,设计一个五个数进行四则运算的计算器 c语言
- linux 时间同步ntp
- 百度前端学院参考答案:第二十五天到第二十七天 倒数开始 滴答滴 滴答滴(2)...
- 网站开启 IPv6 访问,测试是否支持 IPV6
- Graph DataBase介绍
- Javawbe的实战案例
- 解决VS2017安装一直卡在正在下载
- 超详细的JavaScript对象分享,看完就会了
- 分块上(下)三角矩阵的行列式
- vue 仿网易云音乐项目
- 光纤光猫连接自己路由器的设定
- 我的世界服务器清垃圾文件,我的世界:五大处理“垃圾”方法,我选择懒人方法,你会如何选?...
- OAuth2+JWT新一代认证技术
- 操作系统-------OS概述
- Android手机通讯录
- java enhancer_执行trace命令抛异常,Enhancer error,java.lang.ClassFormatError: null
热门文章
- Ubuntu上使用octopress+github建立个人博客
- LUA实现单词替换功能
- nginx和apache的伪静态区别
- JavaScript 图片的上传前预览(兼容所有浏览器)
- 用Shell脚本在推出的RAC节点上批量部署32个Oracle11gR2 RAC备份恢复案例场景的方法PART2...
- shell 远程协助协助(转载)
- GitHub学生包的介绍与申请
- Visual Studio Code Go插件配置选项
- 网络协议 8 - TCP协议(上):性恶就要套路深
- 【例题 6-21 UVA - 506】System Dependencies