输出事件发射器的角度2单元测试

时间:2017-05-11 09:38:50

标签: angular unit-testing

我正在为输出事件发射器编写简单的单元测试用例:

describe('ConfirmDeclineComponent',()=> {

    function findElement(fixture: ComponentFixture<ConfirmDeclineComponent>, selector: string): any {
    return fixture.debugElement.query(By.css(selector)).nativeElement;
}it('should emit on click back button', fakeAsync((): void => {

     let fixture = TestBed.createComponent(ConfirmDeclineComponent);
     let instance: ConfirmDeclineComponent = fixture.componentInstance;
    spyOn(instance.submitConfirmInfoParent, 'emit');
    let button = findElement(fixture, '.back-btn')
     button.dispatchEvent(new Event('click'));
    fixture.detectChanges();
    tick();

    expect(comp.submitConfirmInfoParent.emit).toHaveBeenCalled();
}));});

ConfirmDeclineComponent.ts:

    @Component({
selector: "decline",
templateUrl: "<button md-raised-button class="back-btn" color="accent" (click)="navigateToVinValidation()" style="margin-right:25px;padding:0px">
                <md-icon class="fa fa-arrow-left"></md-icon>
                {{'COMMON.BACK' | translate}}
            </button>"})`export class ConfirmDeclineComponent implements OnInit {
@Output() navigateToVinValidationParent = new EventEmitter();
ngOnInit() {}
navigateToVinValidation() {
    this.navigateToVinValidationParent.emit();
}}`

我收到以下错误:

**Expected spy emit to have been called**

0 个答案:

没有答案