import { async, ComponentFixture, TestBed } from "@angular/core/testing";
import { AlbumsComponent } from "./albums.component";
import { AlbumDetailsComponent } from "./album-details/album-details.component";
import { AppRoutingModule } from "../app.routing.module";
import { PhotosComponent } from "../photos/photos.component";
import { PhotoDetailComponent } from "../photos/photo-detail/photo-detail.component";
import { AlbumService } from "../service/album.service";
import {
HttpClientTestingModule,
HttpTestingController
} from "@angular/common/http/testing";
import { Album } from "../model/album.model";
describe("AlbumsComponent", () => {
let component: AlbumsComponent;
let fixture: ComponentFixture<AlbumsComponent>;
let albumService: AlbumService;
let httpMock: HttpTestingController;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AlbumsComponent,
AlbumDetailsComponent,
PhotosComponent,
PhotoDetailComponent
],
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 be created", () => {
expect(albumService).toBeTruthy();
});
it("should create", () => {
expect(component).toBeTruthy();
});
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);
});
});
i got the following error--
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>
there were no compilation errors. here is my service class
album.service.ts
import { Injectable } from "@angular/core";
import { HttpClient, HttpParams } from "@angular/common/http";
import { map, tap } from "rxjs/operators";
import { Album } from "../model/album.model";
import { Observable } from "rxjs";
import { UserName } from "../model/user.model";
@Injectable({ providedIn: "root" })
export class AlbumService {
constructor(private http: HttpClient) {}
albumUrl = "http://jsonplaceholder.typicode.com/albums";
userUrl = "http://jsonplaceholder.typicode.com/users?id=";
photoUrl = "http://jsonplaceholder.typicode.com/photos";
//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);
})
);
}
//get the user name of the particular album with the help of userId property in albums
fetchUsers(id: String): Observable<any> {
//let userId = new HttpParams().set("userId", id);
return this.http.get(this.userUrl + id);
}
//get the photos of a particular album using the albumId
fetchPhotos(id: string): Observable<any> {
let selectedId = new HttpParams().set("albumId", id);
return this.http.get(this.photoUrl, {
params: selectedId
});
}
}
TypeError: httpMock.expectOne is not a function
I am not able to understand why expectOne is not a function. I am new to angular and i am learning angular testing.here is the link of the youtube video which i followed https://www.youtube.com/watch?v=4JVnSkR04tM&t=376s
Aucun commentaire:
Enregistrer un commentaire