|
| 1 | +/* tslint:disable:no-unused-variable */ |
| 2 | + |
| 3 | +import { |
| 4 | + HttpClientTestingModule, |
| 5 | + HttpTestingController |
| 6 | +} from '@angular/common/http/testing'; |
| 7 | +import { of } from 'rxjs/observable/of'; |
| 8 | +import { TestBed } from '@angular/core/testing'; |
| 9 | +import { RemoteService } from './remote.service'; |
| 10 | + |
| 11 | +describe('RemoteService (fake call with Jasmine)', () => { |
| 12 | + let service: RemoteService; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + TestBed.configureTestingModule({ |
| 16 | + imports: [HttpClientTestingModule], |
| 17 | + // providers: [{ provide: RemoteService, useValue: jasmineSpy }] |
| 18 | + providers: [RemoteService] |
| 19 | + }); |
| 20 | + |
| 21 | + // inject the service |
| 22 | + service = TestBed.get(RemoteService); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should have a service instance', () => { |
| 26 | + expect(service).toBeDefined(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should return the mocked data in the subscribe', () => { |
| 30 | + const spy = spyOn(service, 'fetchViaHttp').and.returnValue( |
| 31 | + of({ |
| 32 | + name: 'Juri' |
| 33 | + }) |
| 34 | + ); |
| 35 | + |
| 36 | + // act |
| 37 | + service.fetchViaHttp().subscribe(data => { |
| 38 | + expect(data.name).toBe('Juri'); |
| 39 | + }); |
| 40 | + |
| 41 | + // assert |
| 42 | + expect(spy).toHaveBeenCalled(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should not invoke the error throwing function since we mocked it', () => { |
| 46 | + const emptyFn = () => {}; |
| 47 | + const spy = spyOn(service, 'throwingError').and.callFake(emptyFn); |
| 48 | + |
| 49 | + // act |
| 50 | + service.throwingError(); |
| 51 | + |
| 52 | + // assert |
| 53 | + expect(spy).toHaveBeenCalled(); |
| 54 | + }); |
| 55 | +}); |
0 commit comments