web:javascript:angular:unittests
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
web:javascript:angular:unittests [2019/06/11 02:34] – [Creating a Deep Integration Test] sgariepy | web:javascript:angular:unittests [2022/02/02 00:42] (Version actuelle) – modification externe 127.0.0.1 | ||
---|---|---|---|
Ligne 586: | Ligne 586: | ||
+ | ===== Integration Testing of Services ===== | ||
+ | On veut tester l' | ||
+ | <code javascript> | ||
+ | import { TestBed } from " | ||
+ | import { HeroService } from " | ||
+ | import { MessageService } from " | ||
+ | import { HttpClientTestingModule, | ||
+ | |||
+ | describe(' | ||
+ | let mockMessageService; | ||
+ | let httpTestingController: | ||
+ | let service: HeroService; | ||
+ | |||
+ | beforeEach(() => { | ||
+ | mockMessageService = jasmine.createSpyObj([' | ||
+ | |||
+ | TestBed.configureTestingModule({ | ||
+ | imports: [ HttpClientTestingModule ], | ||
+ | providers: [ | ||
+ | HeroService, | ||
+ | {provide: MessageService, | ||
+ | ] | ||
+ | }); | ||
+ | |||
+ | httpTestingController = TestBed.get(HttpTestingController); | ||
+ | service = TestBed.get(HeroService); | ||
+ | }); | ||
+ | }); | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Implementing a Test with Mocked HTTP ===== | ||
+ | |||
+ | |||
+ | Ce qu'on veut tester, c'est la méthode suivante dans le service: | ||
+ | |||
+ | |||
+ | <code javascript> | ||
+ | getHero(id: number): Observable< | ||
+ | const url = `${this.heroesUrl}/ | ||
+ | | ||
+ | return this.http.get< | ||
+ | tap(_ => this.log(`fetched hero id=${id}`)), | ||
+ | catchError(this.handleError< | ||
+ | ); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | Alors, un exemple de test serait : | ||
+ | |||
+ | <code javascript> | ||
+ | it(' | ||
+ | service.getHero(4).subscribe(); | ||
+ | |||
+ | const req = httpTestingController.expectOne(' | ||
+ | req.flush({id: | ||
+ | httpTestingController.verify(); | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | ==== Obtenir l' | ||
+ | |||
+ | |||
+ | Pour obtenir une instance de '' | ||
+ | |||
+ | <code javascript> | ||
+ | import { TestBed, inject } from ' | ||
+ | |||
+ | // ... | ||
+ | |||
+ | it(' | ||
+ | // ... | ||
+ | })); | ||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | |||
+ | ====== Testing DOM Interaction and Routing Components ====== | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ====== Sources ====== | ||
+ | |||
+ | * [[https:// | ||
web/javascript/angular/unittests.1560213296.txt.gz · Dernière modification : 2022/02/02 00:43 (modification externe)