【问题标题】:Angular 7 spyon from is not working in unit testing?Angular 7 spyon 不能在单元测试中工作?
【发布时间】:2019-07-11 11:03:28
【问题描述】:

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

搜索问题

SpyOn in Angular testing

spyOn not working in Http Interceptor in Angular 6

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

错误:

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

【问题讨论】:

    标签: angular unit-testing http jasmine karma-jasmine


    【解决方案1】:

    应该是of而不是from

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

    其实你可以直接使用下面这样的,我看到of是直接导入的。

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

    【讨论】:

    • 我正在使用这个spyOn(service,'getproduct').and.returnValue(Observable.of(GetData));,但仍然无法正常工作。使用后spyOn(service,'getproduct').and.returnValue(of(GetData));.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 2017-10-15
    • 2020-04-08
    相关资源
    最近更新 更多