album.spe c .ts
describe("AlbumsComponent", () => {
let component: AlbumsComponent;
let fixture: ComponentFixture<AlbumsComponent>;
let albumService: AlbumService;
let httpMock: HttpTestingController;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
......
],
imports: [AppRoutingModule, HttpClientTestingModule],
providers: [AlbumService, HttpTestingController]
}).compileComponents();
albumService = TestBed.get(AlbumService);
httpMock = TestBed.get(HttpTestingController);
}));
beforeEach(() => {
fixture = TestBed.createComponent(AlbumsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it("should retrieve posts from the API via GET", () => {
const dummyAlbums: Album[] = [
{
userId: 1,
userName: "Arpan",
id: 1,
title: "quidem molestiae enim"
},
{
userId: 1,
userName: "Banerjee",
id: 2,
title: "sunt qui excepturi placeat culpa"
}
];
albumService.fetchAlbums().subscribe(albums => {
expect(albums.length).toBe(2);
expect(albums).toEqual(dummyAlbums);
});
const request = httpMock.expectOne(
"http://jsonplaceholder.typicode.com/albums"
);
expect(request.request.method).toBe("GET");
request.flush(dummyAlbums);
});
});
я получил следующую ошибку -
10 specs, 1 failure, randomized with seed 94637
Spec List | Failures
AlbumsComponent > should retrieve posts from the API via GET
**TypeError: httpMock.expectOne is not a function**
at <Jasmine>
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/albums/albums.component.spec.ts:67:30)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:359:1)
at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:308:1)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:358:1)
at Zone.run (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:124:1)
at runInTestZone (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:561:1)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:576:1)
at <Jasmine>
не было ошибок компиляции. вот мой класс обслуживания
album.service.ts
@Injectable({ providedIn: "root" })
export class AlbumService {
constructor(private http: HttpClient) {}
albumUrl = "http://jsonplaceholder.typicode.com/albums";
//get the album title along with the user name
**fetchAlbums(): Observable<any> {
return this.http.get<Album[]>(this.albumUrl).pipe(
tap(albums => {
albums.map((album: { userId: String; userName: String }) => {
this.fetchUsers(album.userId).subscribe((user: any) => {
album.userName = user[0].username;
});
});
// console.log(albums);
})
);
}**
Ошибка типа: httpMock.expectOne не является функцией
Я не могу чтобы понять, почему ожидаемо не функция. Я новичок в angular и изучаю angular тестирование. Здесь есть ссылка на видео YouTube, по которому я следовал https://www.youtube.com/watch?v=4JVnSkR04tM&t=376s