zoukankan      html  css  js  c++  java
  • [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);
    });
  • 相关阅读:
    Windows Azure 网站开发Stacks支持
    AzureDev 社区活动获奖者公布
    Android 改变窗口标题栏的布局
    cocos2d-x游戏开发系列教程-超级玛丽01-前言
    cocos2dx进阶学习之CCObject
    基于visual Studio2013解决算法导论之055拓扑排序
    查看某文件夹内文件大小
    vmstat命令
    uname 命令
    iostat命令
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7646550.html
Copyright © 2011-2022 走看看