簡體   English   中英

Angular 7 spyon無法在單元測試中工作?

[英]Angular 7 spyon from is not working in unit testing?

首先,我嘗試了與此主題相關的所有問題和答案。 另外,我嘗試了相關問題並嘗試解決,但沒有成功。 因此,請仔細閱讀我的問題。

搜索問題

Angular測試中的SpyOn

spyOn在Angular 6中的Http Interceptor中不起作用

基本上,我的單元測試調用調用一個服務和比較數據的部件上的方法,但以某種方式是錯誤的存在。

錯誤:

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

在此處輸入圖片說明

接口

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

服務

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;
      }
    );

  }

}

單元測試代碼

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);
  });
});

它應該是 ,而不是

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

實際上,您可以直接使用下面的like,因為我看到of是直接導入的。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM