6: Get Data from a Server
authorKai Moritz <kai@juplo.de>
Sun, 17 May 2020 14:11:12 +0000 (16:11 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 17 May 2020 14:11:12 +0000 (16:11 +0200)
h) Heroes and HTTP - Get hero by id (Fixed tests)

src/app/hero.service.spec.ts
src/app/hero.service.ts

index de1c518..843d8a7 100644 (file)
@@ -2,10 +2,12 @@ import { TestBed} from '@angular/core/testing';
 import { TestScheduler } from 'rxjs/testing';
 
 import { HeroService } from './hero.service';
 import { TestScheduler } from 'rxjs/testing';
 
 import { HeroService } from './hero.service';
-import { HttpClientTestingModule } from '@angular/common/http/testing';
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { Hero } from './hero';
 
 describe('HeroService', () => {
   let service: HeroService;
 
 describe('HeroService', () => {
   let service: HeroService;
+  let httpTestingController: HttpTestingController;
   let scheduler: TestScheduler;
 
   beforeEach(() =>  {
   let scheduler: TestScheduler;
 
   beforeEach(() =>  {
@@ -13,24 +15,45 @@ describe('HeroService', () => {
       imports: [ HttpClientTestingModule ]
     });
     service = TestBed.inject(HeroService);
       imports: [ HttpClientTestingModule ]
     });
     service = TestBed.inject(HeroService);
+    httpTestingController = TestBed.inject(HttpTestingController);
     scheduler = new TestScheduler(((actual, expected) => {
       expect(actual).toEqual(expected);
     }));
   });
 
     scheduler = new TestScheduler(((actual, expected) => {
       expect(actual).toEqual(expected);
     }));
   });
 
+  afterEach(() => {
+    // After every test, assert that there are no more pending requests.
+    httpTestingController.verify();
+  });
+
   it('should be created', () => {
     expect(service).toBeTruthy();
   });
 
   it('should be created', () => {
     expect(service).toBeTruthy();
   });
 
-  it('get for id=0 should return an empty Observable', () =>
-    scheduler.run(({expectObservable}) => {
-      expectObservable(service.getHero(0)).toBe('(|)');
-    })
-  );
-
-  it('get for id=11 should return an Observable, that contains the hero with id 11', () =>
-    scheduler.run(({expectObservable}) => {
-      expectObservable(service.getHero(11)).toBe('(a|)', { a: { id: 11, name: 'Dr Nice' }});
-    })
-  );
+  it('get for id=0 should return an empty Observable', () => {
+    service.getHero(0).subscribe(fail, fail);
+
+    // 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');
+
+    // Respond with the mock heroes
+    req.flush('deliberate 404 error', {status: 404, statusText: 'Not Found'});
+  });
+
+  it('get for id=11 should return an Observable, that contains the hero with id 11', () => {
+    const expectedHero: Hero = {id: 11, name: 'Dr Nice'};
+
+    service.getHero(11).subscribe(
+      hero => expect(hero).toEqual(expectedHero, 'should return expected heroes'),
+      fail
+    );
+
+    // 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');
+
+    // Respond with the mock heroes
+    req.flush(expectedHero);
+  });
 });
 });
index a0b5d5b..75a002b 100644 (file)
@@ -12,7 +12,7 @@ import { catchError, tap } from 'rxjs/operators';
 })
 export class HeroService {
 
 })
 export class HeroService {
 
-  private heroesUrl = 'api/heroes';  // URL to web api
+  heroesUrl = 'api/heroes';  // URL to web api
 
   constructor(
     private http: HttpClient,
 
   constructor(
     private http: HttpClient,