From 29ece987444fb92f5701a41537a4ab870a318654 Mon Sep 17 00:00:00 2001 From: Sam Severance Date: Mon, 24 May 2021 11:47:27 -0400 Subject: [PATCH] docs: asynchronous test spy method (#42274) fixes two `HeroService` tests that were synchronously testing an asynchronous spy method PR Close #42274 --- .../testing/src/app/model/hero.service.spec.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/aio/content/examples/testing/src/app/model/hero.service.spec.ts b/aio/content/examples/testing/src/app/model/hero.service.spec.ts index c53ce95668..936936a5b8 100644 --- a/aio/content/examples/testing/src/app/model/hero.service.spec.ts +++ b/aio/content/examples/testing/src/app/model/hero.service.spec.ts @@ -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