Encountering an issue while running unit test cases with jasmine-karma in Angular 7. The error received is:
ProjectManagementComponent should use the ProjectList from the service
TypeError: this.ProjectManagementService.getProject is not a function
If I switch from useValue to useClass, another error occurs:
[object ErrorEvent] thrown
Despite trying various options, I am unable to find a solution online.
app.component.spec.ts
describe('ProjectManagementComponent', () => {
let comp: ProjectManagementComponent;
let fixture: ComponentFixture<ProjectManagementComponent>;
let de: DebugElement;
let el: HTMLElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ProjectManagementComponent],
imports: [HttpClientModule, RouterTestingModule, RouterModule, NgbModule, NgxPaginationModule, FormsModule, ReactiveFormsModule, BrowserModule,],
providers: [{ provide: ProjectManagementService, useClass: ProjectManagementService },
{provide: ProductsService, useClass: ProductsService}]
})
.compileComponents().then(() => {
fixture = TestBed.createComponent(ProjectManagementComponent);
comp = fixture.componentInstance;
de = fixture.debugElement.query(By.css('form[id=addProjectCreationData]'))
el =de.nativeElement;
});
}));
it("should use the ProjectList from the service", () => {
console.log("Create a Project Service")
const projectService = fixture.debugElement.injector.get(ProjectManagementService);
fixture.detectChanges();
expect(projectService.getProject()).toEqual(comp.getResponse);
});
});
app.component.service.stub.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { config } from "config";
const baseUrl: string = config.url;
@Injectable()
export class ProjectManagementServiceStub {
constructor(private http: HttpClient) { }
getProject(url) :Observable<any>{
return this.http.get(url )
.pipe(map(Response => Response))
}
}