Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何模拟可观测返回角服务?_Angular_Observable_Karma Runner - Fatal编程技术网

Angular 如何模拟可观测返回角服务?

Angular 如何模拟可观测返回角服务?,angular,observable,karma-runner,Angular,Observable,Karma Runner,this.variable=this.service.getMydata() 上面的服务返回可观察的,对于我订阅的变量,如下所示 this.variable.subscribe(数据=>{My logic here}) 如何在angular 6中对此进行正确的单元测试 试试这个: const mockResponse = { // ... }; spyOn(service, 'getMydata').and.returnValue(of(mockResponse)); 模拟服务和方法以返

this.variable=this.service.getMydata()

上面的服务返回可观察的,对于我订阅的变量,如下所示

this.variable.subscribe(数据=>{My logic here})

如何在angular 6中对此进行正确的单元测试

试试这个:

const mockResponse = {
  // ...
};

spyOn(service, 'getMydata').and.returnValue(of(mockResponse));

模拟服务和方法以返回可观察的

    const mockService = {
       getMydata: {
          return of(<Response>)
       }
    }

   TestBed.configureTestingModule({
       declarations: [...],
       providers: [{provide: Service, useValue: mockService}]
   })
const mockService={
getMydata:{
返回()
}
}
TestBed.configureTestingModule({
声明:[……],
提供者:[{provide:Service,useValue:mockService}]
})

我无法在subscribe as data中获得响应