I finished the Angular tour of heroes and started writing tests for it. Some were pretty easy to write, but when I try to get data from server it just doesn't work. I have read about testing controller, defer, marbles, schedulers and that kind of stuff, but I still have no idea how to proceed. I either get permanent test failures or the test passes with 'SPEC HAS NO EXPECTATIONS', which also brings nothing as it's just essentially an empty test.
//Test
it('#updateHero() works', waitForAsync(inject([HeroService], (service: HeroService) => {
let testHero = {
id: 42,
name: 'TestHero'
} as Hero
service.updateHero(testHero).subscribe()
service.getHero(42).subscribe(hero => expect(hero.name).toBe('TestHero'))
})));
//service
getHero(id: number): Observable<Hero> {
const url = `${this.heroesUrl}/${id}`
return this.http.get<Hero>(url).pipe(
tap(_ => this.messageService.add(`fetched hero id=${id}`)),
catchError(this.handleError<Hero>(`getHero id=${id}`)))}
updateHero(hero: Hero): Observable<any> {
return this.http.put(this.heroesUrl, hero, this.httpOptions).pipe(
tap(_ => this.messageService.add(`updated hero id=${hero.id}`)),
catchError(this.handleError<any>('updateHero'))
)}
In some other tests I get the same problem, they just don't get anything from my services, however the actual application works perfectly.
My colleagues didn't manage to actually answer my question, except for some strange decisions like mocking the whole server response.