简体   繁体   中英

Angular 7 spyon from is not working in unit testing?

Firstly, I tried all the questions & answers related to this topic. Additionally and I tried related questions and try to solve it but no success. So please read my question thoroughly.

Search Questions :

SpyOn in Angular testing

spyOn not working in Http Interceptor in Angular 6

Basically, my unit test calls a method on a component that calls a service and compares to data but somehow from is error are there.

Error:

Property 'from' does not exist on type 'typeof Observable'.ts(2339)

在此处输入图片说明

interface

export interface servicecasemodel {
    _id:string,
    propertyName:string,
    propertyDesc:string,
    propertyType:number
}

service

import { Injectable } from '@angular/core';
import { servicecasemodel } from 'src/app/service-test-case/model/servicecasemodel';
import { HttpClient } from '@angular/common/http';
import { Observable,of } from 'rxjs';
import { catchError } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class ServicecaseService {

  constructor( private http:HttpClient) { }

   url:string = 'http://XXX.XXX.X.X:5000/api/property';
  getproduct():Observable<servicecasemodel[]> {
    return this.http.get<servicecasemodel[]>(this.url);

  }

}

Component.ts

import { Component, OnInit } from '@angular/core';
import {ServicecaseService} from 'src/app/service-test-case/service/servicecase.service';
import { servicecasemodel } from './model/servicecasemodel';

@Component({
  selector: 'app-servicce-test-case',
  templateUrl: './service-test-case.component.html',
  styleUrls: ['./service-test-case.component.css']
})
export class ServiceTestCaseComponent implements OnInit {

  product:servicecasemodel[];
  constructor( private service_instance:ServicecaseService) { }

  ngOnInit() {
   this.service_instance.getproduct().subscribe(
      (responce:servicecasemodel[]) => {
        console.log(responce);
        this.product = responce;
      }
    );

  }

}

Unit Test Code

import {ServicecaseService} from 'src/app/service-test-case/service/servicecase.service';
import { ServiceTestCaseComponent } from './service-test-case.component';
import { servicecasemodel } from './model/servicecasemodel';
import { Observable,from,of, observable,throwError } from 'rxjs';

describe('ServiceTestCaseComponent', () => {
  let component :ServiceTestCaseComponent;
  let service: ServicecaseService;
  const Getdata :servicecasemodel[] =[
    {
      _id:'1',
      propertyName:'Pro Nmae',
      propertyDesc:'Pro Desc',
      propertyType:1
    },
    {
      _id:'2',
      propertyName:'Pro Nmae 2',
      propertyDesc:'Pro Desc 2',
      propertyType:3
    },
  ];


  beforeEach(() => {

    service = new ServicecaseService(null);
    component = new ServiceTestCaseComponent(service);

  });

  it('should set property with the items returned',() =>{

      spyOn(service,'getproduct').and.callFake(() => { 
      return Observable.from([Getdata]);
    }); 


    // Make actual call
    component.ngOnInit();
    //expect(service).toBeTruthy();
    expect(component.product).toEqual(Getdata);
  });
});

It should be of instead of from

spyOn(service,'getproduct').and.returnValue(Observable.of(GetData));

Infact, you can directly use of like below as I saw that of is directly imported.

 spyOn(service,'getproduct').and.returnValue(of(GetData));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM