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);
+ });
});
});