import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { ReactiveFormsModule } from '@angular/forms'; import { Router } from '@angular/router'; import { NgZorroAntdModule } from 'ng-zorro-antd'; import { of } from 'rxjs/observable/of'; import { AuthService } from '../../shared/services/auth.service'; import { LoginComponent } from './login.component'; describe('LoginComponent', () => { let component: LoginComponent; let fixture: ComponentFixture; let asSpy: AuthServiceSpy; const routerSpy = createRouterSpy(); class AuthServiceSpy { isLoggedIn = false; login = (logForm?) => of({ code: 200 }); } beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ ReactiveFormsModule, NgZorroAntdModule ], declarations: [ LoginComponent ], providers: [ { provide: AuthService, useClass: AuthServiceSpy }, { provide: Router, useValue: routerSpy } ] }) .compileComponents().then(() => { fixture = TestBed.createComponent(LoginComponent); component = fixture.componentInstance; }); })); beforeEach(() => { asSpy = TestBed.get(AuthService); }); it('should create', () => { expect(component).toBeTruthy(); }); it('should login in', () => { asSpy.login().subscribe(data => { console.log('should login in', data); }); }); }); function createRouterSpy() { return jasmine.createSpyObj('Router', ['navigate']); }