mercredi 22 avril 2020

Argument of type 'Observable

This is my testing file i want to create mock service for unit testings the observable is not returning the fake value. Please help me to solve this issue and also please explain me what's wrong in 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 is 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');
  }

This file is 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[];
}

Aucun commentaire:

Enregistrer un commentaire