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);
    });
  • 相关阅读:
    Python 面向对象
    Python __str__()
    数据降维
    高并发相关概念
    centos7下安装kubernetes1.18
    OB-运行日志
    OB-租户(Tenant)管理
    OB-资源管理(Resource Unit/Pool)
    [转载]-基于 VMWARE Oracle Linux7.9 安装 Oracle19c RAC 详细配置方案
    OB-管理oceanbase集群参数
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7646550.html
Copyright © 2011-2022 走看看