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的更多相关文章

  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. Unity 框架(一)

    当项目需求中,后期可能接入多种输入设备的时候,可以借鉴一下以下代码 using System.Collections; using System.Collections.Generic; using ...

  2. 【Henu ACM Round#19 A】 Vasya the Hipster

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟题. 两个一起用->min(a,b); 剩下的除2加上去就好 [代码] #include <bits/stdc++. ...

  3. 洛谷 P3152 正整数序列

    P3152 正整数序列 题目描述 kkk制造了一个序列,这个序列里的数全是由正整数构成的.你别认为她的数列很神奇——其实就是1, 2, …, n而已.当然,n是给定的.kkk的同学lzn认为0是一个好 ...

  4. 什么是 &quot;署名-非商业性使用-同样方式共享&quot;

    什么是 "署名-非商业性使用-同样方式共享" 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致& ...

  5. Codeforces Round #313 C. Gerald&#39;s Hexagon(放三角形)

    C. Gerald's Hexagon time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. Java 实现状态(State)模式

    /** * @author stone */ public class WindowState { private String stateValue; public WindowState(Stri ...

  7. django 笔记3

    FBV function base view url.py index -> 函数名 view.py def 函数(requset): ... CBV class base view /inde ...

  8. Linux PuTTY 更改字体

    Linux PuTTY默认的字体比较小看着比较不舒服,Linux PuTTY的字体更改与Windows下的设置有所不同 1.Linux PuTTY默认的字体 ,Font used for ordina ...

  9. Linux Cgroups

    目录 Linux Cgroups Cgroups中的三个组件 三个组件的关系 Kernel接口 Docker是如何使用Cgroups的 Go语言实现Cgroups限制容器资源 Linux Cgroup ...

  10. CMDB学习之八,完成所有资产采集信息的收集

    #!/usr/bin/env python # -*- coding:utf-8 -*- import traceback from .base import BasePlugin from lib. ...