[英]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 Angular测试中的SpyOn
spyOn not working in Http Interceptor in Angular 6 spyOn在Angular 6中的Http Interceptor中不起作用
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 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. 实际上,您可以直接使用下面的like,因为我看到of
是直接导入的。
spyOn(service,'getproduct').and.returnValue(of(GetData));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.