From: Kai Moritz Date: Fri, 8 May 2020 07:34:23 +0000 (+0200) Subject: Fixed automatically generated unit-tests X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=85f4c0710394f70f840038f5b3185e3adb8981e4;p=examples%2Fangular-tour-of-heroes Fixed automatically generated unit-tests * Invested only minimal efforts * Only fixed current errors * (Nearly) no additional tests were added --- diff --git a/src/app/app.component.spec.ts b/src/app/app.component.spec.ts index ded94c9..38559f2 100644 --- a/src/app/app.component.spec.ts +++ b/src/app/app.component.spec.ts @@ -1,11 +1,20 @@ import { TestBed, async } from '@angular/core/testing'; import { AppComponent } from './app.component'; +import { Component } from '@angular/core'; + +@Component({selector: 'router-outlet', template: ''}) +class RouterOutletStubComponent { } + +@Component({selector: 'app-messages', template: ''}) +class MessagesStubComponent {} describe('AppComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ - AppComponent + AppComponent, + RouterOutletStubComponent, + MessagesStubComponent ], }).compileComponents(); })); @@ -16,16 +25,23 @@ describe('AppComponent', () => { expect(app).toBeTruthy(); }); - it(`should have as title 'angular-tour-of-heroes'`, () => { + it(`should have as title 'Tour of Heroes'`, () => { const fixture = TestBed.createComponent(AppComponent); const app = fixture.debugElement.componentInstance; - expect(app.title).toEqual('angular-tour-of-heroes'); + expect(app.title).toEqual('Tour of Heroes'); + }); + + it(`should render the title 'Tour of Heroes'`, () => { + const fixture = TestBed.createComponent(AppComponent); + fixture.detectChanges(); + const compiled = fixture.debugElement.nativeElement; + expect(compiled.querySelector('h1').textContent).toContain('Tour of Heroes'); }); - it('should render title', () => { + it(`should render the link 'Heroes'`, () => { const fixture = TestBed.createComponent(AppComponent); fixture.detectChanges(); const compiled = fixture.debugElement.nativeElement; - expect(compiled.querySelector('.content span').textContent).toContain('angular-tour-of-heroes app is running!'); + expect(compiled.querySelector('nav > a').textContent).toContain('Heroes'); }); }); diff --git a/src/app/heroes/heroes.component.spec.ts b/src/app/heroes/heroes.component.spec.ts index 66518e4..49cddf7 100644 --- a/src/app/heroes/heroes.component.spec.ts +++ b/src/app/heroes/heroes.component.spec.ts @@ -2,13 +2,31 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { HeroesComponent } from './heroes.component'; +import { Observable, of } from 'rxjs'; +import { Hero } from '../hero'; +import { HEROES } from '../mock-heroes'; +import { HeroService } from '../hero.service'; +import { MessageService } from '../message.service'; + +class MockHeroService { + getHeroes() : Observable { + return of(HEROES); + } +} + +class MockMessageService {} + describe('HeroesComponent', () => { let component: HeroesComponent; let fixture: ComponentFixture; beforeEach(async(() => { TestBed.configureTestingModule({ - declarations: [ HeroesComponent ] + declarations: [ HeroesComponent ], + providers: [ + { provide: HeroService, useClass: MockHeroService }, + { provide: MessageService, useClass: MockMessageService } + ] }) .compileComponents(); }));