When you testing Component rendering, you often needs to call:

  1. fixture.detectChanges();

For example:

  1. it('should display original title', () => {
  2. fixture.detectChanges();
  3. expect(el.textContent).toContain(comp.title);
  4. });
  5.  
  6. it('should display a different test title', () => {
  7. comp.title = 'Test Title';
  8. fixture.detectChanges(); // After change the prop of comp instance, call detectChanges()
  9. expect(el.textContent).toContain('Test Title');
  10. });

You can also set auto change detection:

  1. import { ComponentFixtureAutoDetect } from '@angular/core/testing';

Add to providers:

  1. TestBed.configureTestingModule({
  2. declarations: [ BannerComponent ],
  3. providers: [
  4. { provide: ComponentFixtureAutoDetect, useValue: true }
  5. ]
  6. })

Tests wit auto change detection:

  1. it('should display original title', () => {
  2. // Hooray! No `fixture.detectChanges()` needed
  3. expect(el.textContent).toContain(comp.title);
  4. });
  5.  
  6. it('should still see original title after comp.title change', () => {
  7. const oldTitle = comp.title;
  8. comp.title = 'Test Title';
  9. // Displayed title is old because Angular didn't hear the change :(
  10. expect(el.textContent).toContain(oldTitle);
  11. });
  12.  
  13. it('should display updated title after detectChanges', () => {
  14. comp.title = 'Test Title';
  15. fixture.detectChanges(); // detect changes explicitly
  16. expect(el.textContent).toContain(comp.title);
  17. });

[Angular & Unit Testing] Automatic change detection的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [Angular Unit Testing] Testing Component methods

    import {ComponentFixture, TestBed} from '@angular/core/testing'; import {BrowserDynamicTestingModule ...

  5. [Angular Unit Testing] Testing Services with dependencies

    import { Http, Response, ResponseOptions } from '@angular/http'; import { TestBed } from '@angular/c ...

  6. [Angular Unit Testing] Shallow Pipe Testing

    import { TestBed, ComponentFixture } from '@angular/core/testing'; import { BrowserDynamicTestingMod ...

  7. [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 ...

  8. [Unit Testing] Angular Unit Testing, ui-router, httpbackend and spy

    // backend test beforeEach(inject(function (_$compile_, _$httpBackend_, _$rootScope_, _$state_, _Ann ...

  9. [Angular & Unit Testing] TestBed.get vs Injector

    Both what "TestBed.get" & "injector" trying to do is get service for the tes ...

随机推荐

  1. [APIO2010]巡逻(树的直径)

    [APIO2010]巡逻 题目描述 在一个地区中有 n 个村庄,编号为 1, 2, ..., n.有 n – 1 条道路连接着这些村 庄,每条道路刚好连接两个村庄,从任何一个村庄,都可以通过这些道路到 ...

  2. [HAOI2006]旅行(并查集)

    寒假填坑五十道省选题——第五道 [HAOI2006]旅行 题目描述 Z小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光.Z小镇附近共有N个景点(编号为1,2,3,…,N),这些景点被M条道路 ...

  3. ASP.NET WEB API微信支付通知接口,返回xml数据,微信服务器不识别问题

    原文:ASP.NET WEB API微信支付通知接口,返回xml数据,微信服务器不识别问题 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/MrTra ...

  4. 从头认识java-18.2 主要的线程机制(5)-守护线程与非守护线程

    这一章节我们来讨论一下守护线程与非守护线程. 1.什么是守护线程?什么是非守护线程? 非守护线程:Java虚拟机在它全部非守护线程已经离开后自己主动离开. 守护线程:守护线程则是用来服务用户线程的,假 ...

  5. 安卓自己定义对话框及The specified child already has a child问题

    问题:在android开发过程中,有时会在不同情况下遇到同种问题:The specified child already has a parent.You must call removeView() ...

  6. centos 6.5搭建Samba

    Samba共享服务器 先将selinux关闭和防火墙端口号打开 #iptables -A INPUT -p udp -dport -j ACCEPT #iptables -A INPUT -p udp ...

  7. BZOJ 1112 线段树

    思路: 权值线段树 (找中位数用的) 记录下出现的次数和sum 一定要注意 有可能中位数的值有许多数 这怎么办呢 (离散化以后不去重就行了嘛--.) (为什么他们想得那么麻烦) //By Sirius ...

  8. HDU 5358 First One 数学+尺取法

    多校的题,摆明了数学题,但是没想出来,蠢爆了,之前算了半天的s[i][j]的和,其实是积.其实比赛的时候我连log(s[i][j])+1是s[i][j]的位数都没看出来,说出来都丢人. 知道了这个之后 ...

  9. P2038 无线网络发射器选址

    题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129 条东西向街道和129 条南北向街道所形成的网格状,并且相邻 ...

  10. 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 ...