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:
Sam Severance 2021-05-24 11:47:27 -04:00 committed by Zach Arend
parent 080211264f
commit 29ece98744
1 changed files with 12 additions and 6 deletions

View File

@ -20,20 +20,23 @@ describe ('HeroesService (with spies)', () => {
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[] =
[{ id: 1, name: 'A' }, { id: 2, name: 'B' }];
httpClientSpy.get.and.returnValue(asyncData(expectedHeroes));
heroService.getHeroes().subscribe(
heroes => expect(heroes).toEqual(expectedHeroes, 'expected heroes'),
fail
heroes => {
expect(heroes).toEqual(expectedHeroes, 'expected heroes');
done();
},
done.fail
);
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({
error: 'test 404 error',
status: 404, statusText: 'Not Found'
@ -42,8 +45,11 @@ describe ('HeroesService (with spies)', () => {
httpClientSpy.get.and.returnValue(asyncError(errorResponse));
heroService.getHeroes().subscribe(
heroes => fail('expected an error, not heroes'),
error => expect(error.message).toContain('test 404 error')
heroes => done.fail('expected an error, not heroes'),
error => {
expect(error.message).toContain('test 404 error');
done();
}
);
});
// #enddocregion test-with-spies