[Angular & Unit Testing] Automatic change detection
When you testing Component rendering, you often needs to call:
- fixture.detectChanges();
For example:
- it('should display original title', () => {
- fixture.detectChanges();
- expect(el.textContent).toContain(comp.title);
- });
- it('should display a different test title', () => {
- comp.title = 'Test Title';
- fixture.detectChanges(); // After change the prop of comp instance, call detectChanges()
- expect(el.textContent).toContain('Test Title');
- });
You can also set auto change detection:
- import { ComponentFixtureAutoDetect } from '@angular/core/testing';
Add to providers:
- TestBed.configureTestingModule({
- declarations: [ BannerComponent ],
- providers: [
- { provide: ComponentFixtureAutoDetect, useValue: true }
- ]
- })
Tests wit auto change detection:
- it('should display original title', () => {
- // Hooray! No `fixture.detectChanges()` needed
- expect(el.textContent).toContain(comp.title);
- });
- it('should still see original title after comp.title change', () => {
- const oldTitle = comp.title;
- comp.title = 'Test Title';
- // Displayed title is old because Angular didn't hear the change :(
- expect(el.textContent).toContain(oldTitle);
- });
- it('should display updated title after detectChanges', () => {
- comp.title = 'Test Title';
- fixture.detectChanges(); // detect changes explicitly
- expect(el.textContent).toContain(comp.title);
- });
[Angular & Unit Testing] Automatic change detection的更多相关文章
- [Angular Unit Testing] Debug unit testing -- component rendering
If sometime you want to log out the comonent html to see whether the html render correctly, you can ...
- [Angular + Unit Testing] Mock HTTP Requests made with Angular’s HttpClient in Unit Tests
In a proper unit test we want to isolate external dependencies as much as possible to guarantee a re ...
- [Angular & Unit Testing] Testing Component with Store
When using Ngrx, we need to know how to test the component which has Router injected. Component: imp ...
- [Angular Unit Testing] Testing Component methods
import {ComponentFixture, TestBed} from '@angular/core/testing'; import {BrowserDynamicTestingModule ...
- [Angular Unit Testing] Testing Services with dependencies
import { Http, Response, ResponseOptions } from '@angular/http'; import { TestBed } from '@angular/c ...
- [Angular Unit Testing] Shallow Pipe Testing
import { TestBed, ComponentFixture } from '@angular/core/testing'; import { BrowserDynamicTestingMod ...
- [Angular & Unit Testing] Testing a RouterOutlet component
The way to test router componet needs a little bit setup, first we need to create a "router-stu ...
- [Unit Testing] Angular Unit Testing, ui-router, httpbackend and spy
// backend test beforeEach(inject(function (_$compile_, _$httpBackend_, _$rootScope_, _$state_, _Ann ...
- [Angular & Unit Testing] TestBed.get vs Injector
Both what "TestBed.get" & "injector" trying to do is get service for the tes ...
随机推荐
- [APIO2010]巡逻(树的直径)
[APIO2010]巡逻 题目描述 在一个地区中有 n 个村庄,编号为 1, 2, ..., n.有 n – 1 条道路连接着这些村 庄,每条道路刚好连接两个村庄,从任何一个村庄,都可以通过这些道路到 ...
- [HAOI2006]旅行(并查集)
寒假填坑五十道省选题——第五道 [HAOI2006]旅行 题目描述 Z小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光.Z小镇附近共有N个景点(编号为1,2,3,…,N),这些景点被M条道路 ...
- ASP.NET WEB API微信支付通知接口,返回xml数据,微信服务器不识别问题
原文:ASP.NET WEB API微信支付通知接口,返回xml数据,微信服务器不识别问题 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/MrTra ...
- 从头认识java-18.2 主要的线程机制(5)-守护线程与非守护线程
这一章节我们来讨论一下守护线程与非守护线程. 1.什么是守护线程?什么是非守护线程? 非守护线程:Java虚拟机在它全部非守护线程已经离开后自己主动离开. 守护线程:守护线程则是用来服务用户线程的,假 ...
- 安卓自己定义对话框及The specified child already has a child问题
问题:在android开发过程中,有时会在不同情况下遇到同种问题:The specified child already has a parent.You must call removeView() ...
- centos 6.5搭建Samba
Samba共享服务器 先将selinux关闭和防火墙端口号打开 #iptables -A INPUT -p udp -dport -j ACCEPT #iptables -A INPUT -p udp ...
- BZOJ 1112 线段树
思路: 权值线段树 (找中位数用的) 记录下出现的次数和sum 一定要注意 有可能中位数的值有许多数 这怎么办呢 (离散化以后不去重就行了嘛--.) (为什么他们想得那么麻烦) //By Sirius ...
- HDU 5358 First One 数学+尺取法
多校的题,摆明了数学题,但是没想出来,蠢爆了,之前算了半天的s[i][j]的和,其实是积.其实比赛的时候我连log(s[i][j])+1是s[i][j]的位数都没看出来,说出来都丢人. 知道了这个之后 ...
- P2038 无线网络发射器选址
题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129 条东西向街道和129 条南北向街道所形成的网格状,并且相邻 ...
- cf 865 B. Ordering Pizza
B. Ordering Pizza It's another Start[c]up finals, and that means there is pizza to order for the ons ...