代码之家  ›  专栏  ›  技术社区  ›  Raymond Yeh

有棱角的茉莉花期待间谍按钮点击已被调用

  •  1
  • Raymond Yeh  · 技术社区  · 6 年前

    我正在为我的Angular6项目编写单元测试。

    以下是我的代码:

    <div>
     <button (click)="onButtonClick(1)"><button>
     <button (click)="onButtonClick(2)"><button>
     <button (click)="onButtonClick(3)"><button>
    </div>
    

    onButtonClick(key: number) {
      do something
    }
    

    规范ts

    import { async, ComponentFixture, TestBed, fakeAsync } from '@angular/core/testing';
    
    import { PanelButtonsComponent } from './panel-buttons.component';
    import { By } from "@angular/platform-browser";
    
    describe('PanelButtonsComponent', () => {
    let component: PanelButtonsComponent;
    let fixture: ComponentFixture<PanelButtonsComponent>;
    
    beforeEach(async(() => {
      TestBed.configureTestingModule({
        declarations: [PanelButtonsComponent]
      })
        .compileComponents();
    }));
    
    beforeEach(() => {
      fixture = TestBed.createComponent(PanelButtonsComponent);
      component = fixture.componentInstance;
      fixture.detectChanges();
    });
    
    
    
    it("should call onButtonClick ", fakeAsync(() => {
      const onClickMock = spyOn(component, 'onButtonClick');
    
     fixture.debugElement.query(By.css('button')).triggerEventHandler('click', null);
      expect(onClickMock).toHaveBeenCalled();
    }));
    
    
    });
    

    试验结果:

    预期间谍按钮点击已被调用。

    有什么建议可以纠正吗?谢谢你

    更新

    article

    it('should', async(() => {
      spyOn(component, 'onButtonClick');
    
      let button = 
      fixture.debugElement.nativeElement.querySelector('button');
      button.click();
    
      fixture.whenStable().then(() => {
        expect(component.onButtonClick).toHaveBeenCalled();
      })
    }));
    

    更新2:

    在我的html中,有两种click函数会被调用,所以这会导致错误

    <div>
     <button (click)="onButtonClick(1)"><button>
     <button (click)="onButtonClick(2)"><button>
     <button (click)="onButtonClick(3)"><button>
     <button (click)="onResetClick()"><button>
    </div>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Raymond Yeh    6 年前

    我想这就是我问题的答案。

    一开始,点击按钮后会调用不同种类的函数。

    <div>
     <button (click)="onButtonClick(1)"><button>
     <button (click)="onButtonClick(2)"><button>
     <button (click)="onButtonClick(3)"><button>
     <button (click)="onResetClick()"><button>
    

    因此,测试将导致错误。

    it("should call onButtonClick ", fakeAsync(() => {
      const onClickMock = spyOn(component, 'onButtonClick');
    
     fixture.debugElement.query(By.css('button')).triggerEventHandler('click', null);
      expect(onClickMock).toHaveBeenCalled();
    }));
    

    预期间谍按钮点击已被调用。


    因此,通过测试的正确方法应如下所示:

    将类名添加到HTML

    <div>
     <button (click)="onButtonClick(1)" class="normalBtn"><button>
     <button (click)="onButtonClick(2)" class="normalBtn"><button>
     <button (click)="onButtonClick(3)" class="normalBtn"><button>
     <button (click)="onResetClick()" class="restBtn"><button>
    </div>
    

    it('should call onButtonClick', fakeAsync(() => {
        fixture.detectChanges();
        spyOn(component, 'onButtonClick');
        let btn = fixture.debugElement.queryAll(By.css('.normalBtn'));
        for (let i = 0; i < btn.length; i++) {
          btn[i].triggerEventHandler('click', null);
        }
    
        tick(); // simulates the passage of time until all pending asynchronous activities finish
        fixture.detectChanges();
        expect(component.onButtonClick).toHaveBeenCalled();
      }));
    
      it("should call onResetClick ", fakeAsync(() => {
        fixture.detectChanges();
        spyOn(component, 'onResetClick');
        let btn = fixture.debugElement.query(By.css('.resetBtn'));
        btn.triggerEventHandler('click', null);
        tick(); // simulates the passage of time until all pending asynchronous activities finish
        fixture.detectChanges();
        expect(component.onResetClick).toHaveBeenCalled();
      }));