docs: asynchronous test spy method (#42274)
fixes two `HeroService` tests that were synchronously testing an asynchronous spy method PR Close #42274
This commit is contained in:
parent
080211264f
commit
29ece98744
|
@ -20,20 +20,23 @@ describe ('HeroesService (with spies)', () => {
|
||||||
heroService = new HeroService(httpClientSpy as any);
|
heroService = new HeroService(httpClientSpy as any);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return expected heroes (HttpClient called once)', () => {
|
it('should return expected heroes (HttpClient called once)', (done: DoneFn) => {
|
||||||
const expectedHeroes: Hero[] =
|
const expectedHeroes: Hero[] =
|
||||||
[{ id: 1, name: 'A' }, { id: 2, name: 'B' }];
|
[{ id: 1, name: 'A' }, { id: 2, name: 'B' }];
|
||||||
|
|
||||||
httpClientSpy.get.and.returnValue(asyncData(expectedHeroes));
|
httpClientSpy.get.and.returnValue(asyncData(expectedHeroes));
|
||||||
|
|
||||||
heroService.getHeroes().subscribe(
|
heroService.getHeroes().subscribe(
|
||||||
heroes => expect(heroes).toEqual(expectedHeroes, 'expected heroes'),
|
heroes => {
|
||||||
fail
|
expect(heroes).toEqual(expectedHeroes, 'expected heroes');
|
||||||
|
done();
|
||||||
|
},
|
||||||
|
done.fail
|
||||||
);
|
);
|
||||||
expect(httpClientSpy.get.calls.count()).toBe(1, 'one call');
|
expect(httpClientSpy.get.calls.count()).toBe(1, 'one call');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return an error when the server returns a 404', () => {
|
it('should return an error when the server returns a 404', (done: DoneFn) => {
|
||||||
const errorResponse = new HttpErrorResponse({
|
const errorResponse = new HttpErrorResponse({
|
||||||
error: 'test 404 error',
|
error: 'test 404 error',
|
||||||
status: 404, statusText: 'Not Found'
|
status: 404, statusText: 'Not Found'
|
||||||
|
@ -42,8 +45,11 @@ describe ('HeroesService (with spies)', () => {
|
||||||
httpClientSpy.get.and.returnValue(asyncError(errorResponse));
|
httpClientSpy.get.and.returnValue(asyncError(errorResponse));
|
||||||
|
|
||||||
heroService.getHeroes().subscribe(
|
heroService.getHeroes().subscribe(
|
||||||
heroes => fail('expected an error, not heroes'),
|
heroes => done.fail('expected an error, not heroes'),
|
||||||
error => expect(error.message).toContain('test 404 error')
|
error => {
|
||||||
|
expect(error.message).toContain('test 404 error');
|
||||||
|
done();
|
||||||
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
// #enddocregion test-with-spies
|
// #enddocregion test-with-spies
|
||||||
|
|
Loading…
Reference in New Issue