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