X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fapp%2Fhero.service.spec.ts;h=08364db2fdbf0da5b3612036f548d49ee0df5949;hb=c2ce800fc99390d95d4cfcce93f597490f6a948b;hp=082791a7adcdfe503689a9a78896bf7ba84c42a8;hpb=ef15a90ba71bcff7765f089af11e2ad22752a352;p=examples%2Fangular-tour-of-heroes diff --git a/src/app/hero.service.spec.ts b/src/app/hero.service.spec.ts index 082791a..08364db 100644 --- a/src/app/hero.service.spec.ts +++ b/src/app/hero.service.spec.ts @@ -1,12 +1,89 @@ -import { TestBed } from '@angular/core/testing'; +import { TestBed} from '@angular/core/testing'; +import { TestScheduler } from 'rxjs/testing'; import { HeroService } from './hero.service'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; +import { Hero } from './hero'; describe('HeroService', () => { - beforeEach(() => TestBed.configureTestingModule({})); + let service: HeroService; + let httpTestingController: HttpTestingController; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ HttpClientTestingModule ], + providers: [ HeroService ] + }); + service = TestBed.inject(HeroService); + httpTestingController = TestBed.inject(HttpTestingController); + }); + + afterEach(() => { + // After every test, assert that there are no more pending requests. + httpTestingController.verify(); + }); it('should be created', () => { - const service: HeroService = TestBed.get(HeroService); expect(service).toBeTruthy(); }); + + describe('#getHeroes', () => { + + it('should return the expected heroes on success', () => { + + const expectedHeroes: Hero[] = [{id: 11, name: 'Dr Nice'}]; + + service.getHeroes().subscribe( + heroes => expect(heroes).toEqual(expectedHeroes, 'should return expected heroes'), + fail + ); + + const req = httpTestingController.expectOne(service.heroesUrl); + expect(req.request.method).toEqual('GET'); + + req.flush(expectedHeroes); + }); + + it('should return an empty list if the remote server fails', () => { + + service.getHeroes().subscribe( + heroes => expect(heroes).toEqual([], 'should return an empty list'), + fail + ); + + const req = httpTestingController.expectOne(service.heroesUrl); + expect(req.request.method).toEqual('GET'); + + // Respond with the mock heroes + req.flush('deliberate 500 error', {status: 500, statusText: 'Server Error'}); + }); + }); + + describe('#getHero', () => { + + it('should return an empty observable for an invalid id', () => { + + service.getHero(0).subscribe(fail, fail); + + const req = httpTestingController.expectOne(`${service.heroesUrl}/0`); + expect(req.request.method).toEqual('GET'); + + req.flush('deliberate 404 error', {status: 404, statusText: 'Not Found'}); + }); + + it('should return an observable with the requested hero for a valid id', () => { + + const expectedHero: Hero = {id: 11, name: 'Dr Nice'}; + + service.getHero(11).subscribe( + hero => expect(hero).toEqual(expectedHero, 'should return expected heroes'), + fail + ); + + const req = httpTestingController.expectOne(`${service.heroesUrl}/11`); + expect(req.request.method).toEqual('GET'); + + req.flush(expectedHero); + }); + }); });