jest . mock ( 'app/core/util/alert.service' ) ; import { ComponentFixture , TestBed , waitForAsync } from '@angular/core/testing' ; import { AlertService } from 'app/core/util/alert.service' ; import { AlertComponent } from './alert.component' ; describe ( 'Alert Component' , ( ) => { let comp ; let fixture ; let mockAlertService ; beforeEach ( waitForAsync ( ( ) => { TestBed . configureTestingModule ( { declarations : [ AlertComponent ] , providers : [ AlertService ] , } ) . overrideTemplate ( AlertComponent , '' ) . compileComponents ( ) ; } ) ) ; beforeEach ( ( ) => { fixture = TestBed . createComponent ( AlertComponent ) ; comp = fixture . componentInstance ; mockAlertService = TestBed . inject ( AlertService ) ; } ) ; it ( 'Should call alertService.get on init' , ( ) => { comp . ngOnInit ( ) ; expect ( mockAlertService . get ) . toHaveBeenCalled ( ) ; } ) ; it ( 'Should call alertService.clear on destroy' , ( ) => { comp . ngOnDestroy ( ) ; expect ( mockAlertService . clear ) . toHaveBeenCalled ( ) ; } ) ; } ) ;