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 00:01] – [Dealing with Lists of Elements] sgariepy | web:javascript:angular:unittests [2022/02/02 00:42] (Version actuelle) – modification externe 127.0.0.1 | ||
---|---|---|---|
Ligne 334: | Ligne 334: | ||
- | ===== Tester | + | ===== Interaction test ===== |
+ | Dans le component, si on a une fonction tel que: | ||
+ | <code javascript> | ||
+ | delete(hero: | ||
+ | this.heroes = this.heroes.filter(h => h !== hero); | ||
+ | this.heroService.deleteHero(hero).subscribe(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | La troisième ligne, avec le '' | ||
+ | |||
+ | Donc on peut faire un //test d' | ||
+ | |||
+ | < | ||
+ | it(' | ||
+ | mockHeroService.deleteHero.and.returnValue(of(true)) | ||
+ | component.heroes = HEROES; | ||
+ | |||
+ | component.delete(HEROES[2]); | ||
+ | |||
+ | expect(mockHeroService.deleteHero).toHaveBeenCalledWith(HEROES[2]); | ||
+ | }); | ||
+ | </ | ||
====== Shallow Integration Tests ====== | ====== Shallow Integration Tests ====== | ||
Ligne 342: | Ligne 364: | ||
===== Using NO_ERRORS_SCHEMA ===== | ===== Using NO_ERRORS_SCHEMA ===== | ||
+ | |||
+ | La mention '' | ||
Ligne 357: | Ligne 381: | ||
</ | </ | ||
+ | |||
+ | L' | ||
+ | |||
===== Testing Rendered HTML ===== | ===== Testing Rendered HTML ===== | ||
Ligne 448: | Ligne 475: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ===== Mocking Child Components ===== | ||
+ | |||
+ | |||
+ | Créer un mock de component: | ||
+ | |||
+ | |||
+ | <code javascript> | ||
+ | |||
+ | @Component({ | ||
+ | selector: ' | ||
+ | template: '< | ||
+ | }) | ||
+ | class FakeHeroComponent { | ||
+ | @Input() hero: Hero; // Pas obligé de typer ici vu que c'est un mock | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Ajouter '' | ||
+ | |||
+ | <code javascript> | ||
+ | TestBed.configureTestingModule({ | ||
+ | declarations: | ||
+ | HeroesComponent, | ||
+ | FakeHeroComponent | ||
+ | ], | ||
+ | providers: [ | ||
+ | { provide: HeroService, | ||
+ | ], | ||
+ | }); | ||
+ | </ | ||
+ | |||
===== Dealing with Lists of Elements ===== | ===== Dealing with Lists of Elements ===== | ||
+ | |||
+ | <code javascript> | ||
+ | it(' | ||
+ | mockHeroService.getHeroes.and.returnValue(of(HEROES)) | ||
+ | fixture.detectChanges(); | ||
+ | |||
+ | expect(fixture.debugElement.queryAll(By.css(' | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | |||
+ | ====== Deep Integration Tests ====== | ||
+ | |||
+ | ===== Creating a Deep Integration Test ===== | ||
+ | |||
+ | Dans ces tests, nous voulons évaluer l' | ||
+ | |||
+ | |||
+ | Le test de base, ressemble à ceci: | ||
+ | |||
+ | <code javascript> | ||
+ | describe(' | ||
+ | let fixture: ComponentFixture< | ||
+ | let mockHeroService; | ||
+ | let HEROES; | ||
+ | |||
+ | beforeEach(() => { | ||
+ | mockHeroService = jasmine.createSpyObj([' | ||
+ | |||
+ | TestBed.configureTestingModule({ | ||
+ | declarations: | ||
+ | HeroesComponent, | ||
+ | HeroComponent | ||
+ | ], | ||
+ | providers: [ | ||
+ | { provide: HeroService, | ||
+ | ], | ||
+ | schemas: [NO_ERRORS_SCHEMA] | ||
+ | }) | ||
+ | | ||
+ | fixture = TestBed.createComponent(HeroesComponent); | ||
+ | }); | ||
+ | |||
+ | it(' | ||
+ | expect(true).toBe(true); | ||
+ | }); | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | On inclus '' | ||
+ | |||
+ | |||
+ | ===== Finding Elements by Directive ===== | ||
+ | |||
+ | Le component '' | ||
+ | |||
+ | |||
+ | Remarquez qu'on utilise '' | ||
+ | |||
+ | <code javascript> | ||
+ | it(' | ||
+ | mockHeroService.getHeroes.and.returnValue(of(HEROES)); | ||
+ | |||
+ | // run ngOnInit | ||
+ | fixture.detectChanges(); | ||
+ | |||
+ | const heroComponentDEs = fixture.debugElement.queryAll( | ||
+ | By.directive(HeroComponent) | ||
+ | ); | ||
+ | expect(heroComponentDEs.length).toEqual(3); | ||
+ | for (let i = 0; i < heroComponentDEs.length; | ||
+ | expect(heroComponentDEs[i].componentInstance.hero).toEqual(HEROES[i]); | ||
+ | } | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== 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:// | ||
- | La mention '''' | ||
web/javascript/angular/unittests.1560204069.txt.gz · Dernière modification : 2022/02/02 00:43 (modification externe)