X-Git-Url: https://juplo.de/gitweb/?p=examples%2Fangular-tour-of-heroes;a=blobdiff_plain;f=src%2Fapp%2Fhero.service.spec.ts;fp=src%2Fapp%2Fhero.service.spec.ts;h=7730d9fc6cce64d449d53141c3c4c881a6f0bf8a;hp=843d8a7e55818fe6582637744f48236e3867bbab;hb=510245b57541ddce833a1d76ae2953f12f80fb33;hpb=806998ac75abe1082f666c141e0bd3f1a8ae7af1 diff --git a/src/app/hero.service.spec.ts b/src/app/hero.service.spec.ts index 843d8a7..7730d9f 100644 --- a/src/app/hero.service.spec.ts +++ b/src/app/hero.service.spec.ts @@ -30,30 +30,63 @@ describe('HeroService', () => { expect(service).toBeTruthy(); }); - it('get for id=0 should return an empty Observable', () => { - service.getHero(0).subscribe(fail, fail); + describe('#getHeroes', () => { - // HeroService should have made one request to GET heroes from expected URL - const req = httpTestingController.expectOne(`${service.heroesUrl}/0`); - expect(req.request.method).toEqual('GET'); + it('should return the expected heroes on success', () => { - // Respond with the mock heroes - req.flush('deliberate 404 error', {status: 404, statusText: 'Not Found'}); + 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'}); + }); }); - it('get for id=11 should return an Observable, that contains the hero with id 11', () => { - const expectedHero: Hero = {id: 11, name: 'Dr Nice'}; + describe('#getHero', () => { - service.getHero(11).subscribe( - hero => expect(hero).toEqual(expectedHero, 'should return expected heroes'), - fail - ); + it('should return an empty observable for an invalid id', () => { - // HeroService should have made one request to GET heroes from expected URL - const req = httpTestingController.expectOne(`${service.heroesUrl}/11`); - expect(req.request.method).toEqual('GET'); + service.getHero(0).subscribe(fail, fail); - // Respond with the mock heroes - req.flush(expectedHero); + 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); + }); }); });