https://codecraft.tv/courses/angular/unit-testing/change-detection/

ATB:Angular Test Bed

// create component and test fixture
fixture = TestBed.createComponent(LoginComponent);

fixture通过ATB的createComponent方法创建,输入参数是正式的Component.

The fixture as well as holding an instance of the component also holds a reference to something called a DebugElement, this is a wrapper to the low-level DOM element that represents the component’s view, via the debugElement property.

通过fixture.debugElement获取Component view里的元素:

We can get references to other child nodes by querying this debugElement with a By class. The By class lets us query using a number of methods, one is via a CSS class like we have in our example, another way is to request by a type of directive like By.directive(MyDirective).

调用完query之后,如果不显式调用fixture.detectChanges, 则query返回的handle里,是无法获取到视图内容的。

That’s because when Angular first loads no change detection has been triggered and therefore the view doesn’t show either the Login or Logout text.

fixture is a wrapper for our component’s environment so we can control things like change detection.

fixture是Component环境的wrapper,因此在单元测试代码里,我们可以自行控制change detection的触发时机。

it('login button hidden when the user is authenticated', () => {expect(el.nativeElement.textContent.trim()).toBe('');fixture.detectChanges();expect(el.nativeElement.textContent.trim()).toBe('Login');
});

Angular单元测试fixture.detectChanges()相关推荐

  1. Angular jasmine fixture.detectChanges如何触发directive的set方法

    测试代码: import { Component } from '@angular/core'; import { waitForAsync, ComponentFixture, TestBed } ...

  2. 关于jasmine里debugElement.query和fixture.detectChanges的依赖关系

    单元测试代码: import { Component } from '@angular/core'; import { async, ComponentFixture, TestBed } from ...

  3. Angular单元测试ComponentFixture的实例化过程明细

    从angular core里导入单元测试需要的库文件: import { async, ComponentFixture, TestBed } from '@angular/core/testing' ...

  4. [转]Angular 单元测试讲解

    Angular_单元测试 测试分类 按开发阶段划分 按是否运行划分 按是否查看源代码划分 其他 ATDD,TDD,BDD,DDD ATDD TDD BDD DDD Angular单元测试 Karma的 ...

  5. Angular单元测试框架里API toHaveBeenCalledTimes的工作原理

    看这样一段代码: let spiedFirstFocusable = spyOn(keyboardFocusService,'findFirstFocusable').and.returnValue( ...

  6. jasmine没有调用fixture.detectChanges就取不到元素的原因

    如果没有调用fixture.detectChanges, element context的innerHTML里根本就看不到button的源代码: 要获取更多Jerry的原创文章,请关注公众号" ...

  7. Angular 单元测试讲解

    Angular_单元测试 测试分类 按开发阶段划分 按是否运行划分 按是否查看源代码划分 其他 ATDD,TDD,BDD,DDD ATDD TDD BDD DDD Angular单元测试 Karma的 ...

  8. Angular jasmine单元测试框架fixture.detectChanges的实现原理

    源代码: fixture的类型是component-fixture,里面会调用_tick方法: 具体执行的detectChange逻辑,取决于Change Detect reference: Root ...

  9. Angular单元测试里使用fixture.debugElement测试UI界面元素

    场景1:使用fixture.debugElement.queryAll获得页面所有的a标签: fixture.debugElement.queryAll接受一个predicate: 需要的predic ...

最新文章

  1. 常用javascript代码
  2. python leetcode_python实现leetcode中无重复字符的最长字串
  3. 算法设计与分析——算法思想总结
  4. oracle安装pdb报错,oracle12安装后pdb创建的用户无法连接
  5. libsvm 的使用
  6. 【蓝桥杯】2019:最长子序列
  7. POJ3080Blue Jeans
  8. 【最详细】数据结构(C语言版 第2版)课后习题答案全集 严蔚敏 等 编著
  9. STM32实现四驱小车(二)通信任务——遥控器SBUS通信
  10. 清算号和联行号有区别吗?区别是什么?
  11. java调用cmd命令
  12. 计算机上的无线网络开关怎么打开,联想笔记本无线网络开关怎么打开
  13. NetBIOS编程获取本机MAC地址及一个小坑
  14. Golang中的并发:如何使用Goroutines?详细指南
  15. 简述RAKsmart香港服务器的的优势
  16. php源码 拼车网顺风车_最新拼车网PHP源码整站打包—微信拼车+手机拼车+微信双终端+数据同步...
  17. 通过GitHub静态网页托管,十分钟制作一份精美的在线网页简历
  18. 硬件原理图Checklist检查表(通用版)
  19. 【激光测距仪方案】激光测距仪的测量原理及作用方法
  20. Easy3D 孔洞识别

热门文章

  1. 1623: 街道路径条数
  2. [24]CSS3 弹性伸缩布局(上)
  3. 给老师的建议?????
  4. C#微信公众号开发系列教程三(消息体签名及加解密)
  5. 获取服务器信息info
  6. struts2的处理流程
  7. Spring 通过@Import实现Bean的注册
  8. mybatis-plus的概念
  9. Flask-SQLAlchemy插件
  10. Shell脚本对Linux进行文件校验