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