import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { LogComponent } from './log.component'; import { HttpClientTestingModule } from '@angular/common/http/testing'; import { LogService } from './log.service'; import { MockBackend } from '@angular/http/testing'; import { BaseRequestOptions, Http } from '@angular/http'; import 'rxjs/add/observable/of'; import { Observable } from 'rxjs/Observable'; describe('LogComponent', () => { let component: LogComponent; let fixture: ComponentFixture; let logService:LogService; beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ LogComponent ], imports: [HttpClientTestingModule], // providers:[LogService,MockBackend,BaseRequestOptions,{ // porvide:Http,useFactory:(backend:MockBackend,defaultOptions:BaseRequestOptions)=>{ // return new Http(backend,defaultOptions); // }, // deps:[MockBackend,BaseRequestOptions], // }] }) .compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(LogComponent); component = fixture.componentInstance; fixture.detectChanges(); logService=TestBed.get(LogService); }); it('should create', () => { expect(component).toBeTruthy(); }); describe('should test subscribe',()=>{ it('should test ngOnInit method1',()=>{ component.reportType = "Dashboard"; component.ngOnInit(); expect(component.stepNo).toEqual("3"); }) }) it('should test ngOnInit method2',()=>{ component.reportType != "Dashboard"; component.ngOnInit(); expect(component.stepNo).toEqual("7"); }) it('should test subscribe method inside ngOnInit method',()=>{ let spy=spyOn(logService,'getLogData').and.returnValue(Observable.of('you object')); component.ngOnInit(); expect(spy).toHaveBeenCalled(); }) });