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;
- let scheduler: TestScheduler;
+ let httpTestingController: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
- imports: [ HttpClientTestingModule ]
+ imports: [ HttpClientTestingModule ],
+ providers: [ HeroService ]
});
service = TestBed.inject(HeroService);
- scheduler = new TestScheduler(((actual, expected) => {
- expect(actual).toEqual(expected);
- }));
+ httpTestingController = TestBed.inject(HttpTestingController);
+ });
+
+ afterEach(() => {
+ // After every test, assert that there are no more pending requests.
+ httpTestingController.verify();
});
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' }});
- })
- );
+ describe('#getHeroes', () => {
+
+ it('should return the expected heroes on success', () => {
+
+ 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'});
+ });
+ });
+
+ describe('#getHero', () => {
+
+ it('should return an empty observable for an invalid id', () => {
+
+ service.getHero(0).subscribe(fail, fail);
+
+ 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);
+ });
+ });
});