From: Kai Moritz Date: Sun, 17 May 2020 14:32:44 +0000 (+0200) Subject: Added more tests for HeroService X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=510245b57541ddce833a1d76ae2953f12f80fb33;p=examples%2Fangular-tour-of-heroes Added more tests for HeroService --- 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); + }); }); });