I am currently working on setting up a mock service for unit testing, but I am facing an issue where the observable is not returning the expected fake value. Can someone please assist me in resolving this problem and also explain what might be wrong with my code?
all-projects.component.spec.ts
import { ProjectsService } from './../../../../services/projects/projects.service';
import { AllProjectsComponent } from './all-projects.component';
import 'rxjs/add/observable/from';
import { Observable } from 'rxjs/Observable';
describe('allprojectComponent', () => {
let component: AllProjectsComponent;
let service: ProjectsService;
beforeEach(() => {
service = new ProjectsService(null, null);
component = new AllProjectsComponent(service);
});
it('should set projects property with the items returned from the server', () => {
spyOn(service,'getAllprojects').and.returnValue(Observable.from([[1,2,4]]))
})
});
This section corresponds to my service file.
project.service.ts
import { Injectable } from '@angular/core';
import { HttpClient , HttpBackend, HttpParams } from '@angular/common/http';
import { environment } from './../../../../environments/environment';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ProjectsService {
public API = environment.apiUrl;
public newHttp;
constructor( private http: HttpClient, private handler: HttpBackend ) {
this.newHttp = new HttpClient(this.handler);
}
getAllprojects(): Observable<ProjectObject[]> {
return this.http.get<ProjectObject[]>(this.API + '/projects');
}
The following snippet defines my project model. project.model.ts
export interface Docs {
fileType : string,
extension: string
}
export interface Project {
projectType: string;
status: string;
totalBids: number;
_id: string;
clientID: string;
projectName: string;
licenseType: string;
location: string;
description: string;
duration: number;
createdAt: Date;
updatedAt: Date;
__v: number;
}
export interface OpenFaq {
_id: string;
engineerID: string;
question: string;
}
export interface ProjectObject {
project: Project;
openFaq: OpenFaq[];
closedFaq: any[];
doc: any[];
}