Angular service testing: Cannot find name 'asyncData'
I quote from: https://angular.io/guide/testing
The async observable was produced by an asyncData
helper. The asyncData
helper is a utility function that you'll have to write yourself. Or you can copy this one from the sample code:
testing/async-observable-helpers.ts
/** Create async observable that emits-once and completes
* after a JS engine turn */
export function asyncData<T>(data: T) {
return defer(() => Promise.resolve(data));
}
Note: defer
comes from rxjs
, i.e.: import { defer } from 'rxjs';
Change this line:
httpClientSpy.get.and.returnValue(asyncData(expectedHeroes));
to use the Observable operator of()
httpClientSpy.get.and.returnValue(of(expectedHeroes));
This will return an observable that can be subscribed to and will return expectedHeroes. If you are using Angular 6, you can import this directly from rxjs:
import {of} from 'rxjs';
If download sample code from angular docs, you will find 'asyncData' definition in testing>async-observable-helpers.ts file.