Angular 7 spyon来自单元测试?

问题描述 投票:0回答:1

首先,我尝试了与此主题相关的所有问题和答案。此外,我尝试了相关的问题,并试图解决它,但没有成功。所以请仔细阅读我的问题。

搜索问题:

SpyOn in Angular testing

spyOn not working in Http Interceptor in Angular 6

基本上,我的单​​元测试在一个调用服务的组件上调用一个方法,并与数据进行比较,但不知何故来自于错误。

错误:

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

enter image description here

接口

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);
  });
});
angular unit-testing http jasmine karma-jasmine
1个回答
2
投票

它应该是而不是来自

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

事实上,您可以直接使用下面的内容,因为我看到of是直接导入的。

 spyOn(service,'getproduct').and.returnValue(of(GetData));
© www.soinside.com 2019 - 2024. All rights reserved.