Angular2单元测试-测试对象(观察服务变量)

Angular2单元测试-测试对象(观察服务变量),angular,unit-testing,typescript,Angular,Unit Testing,Typescript,我的一个组件中有一个手表,它监视服务中的一个变量(即主题)。(我在这里得到了这个实现的想法:但是,现在我在组件中的所有测试都失败了,因为我需要以某种方式模拟它,但我不知道如何,也找不到任何答案。这是我的代码: 我在构造函数中观察变量变化的组件: constructor(private updateService: UpdateService) { // check if intents are updated in the updateService through upload

我的一个组件中有一个手表,它监视服务中的一个变量(即主题)。(我在这里得到了这个实现的想法:但是,现在我在组件中的所有测试都失败了,因为我需要以某种方式模拟它,但我不知道如何,也找不到任何答案。这是我的代码:

我在构造函数中观察变量变化的组件:

  constructor(private updateService: UpdateService) {

    // check if intents are updated in the updateService through upload component, if yes update training status
    this.intentsSubscription = updateService.intentsChange.subscribe((value) => this.initTrainingStatus());
  }
 public intentsChange: Subject<object> = new Subject<object>();
   .....

    public updateIntents(appId: number) {
        this.requestsService.getAllIntents(appId)
          .subscribe(intents => {
            this.setIntents(intents);
            this.intentsChange.next(intents);
          });
      }
触发变量更改的服务:

  constructor(private updateService: UpdateService) {

    // check if intents are updated in the updateService through upload component, if yes update training status
    this.intentsSubscription = updateService.intentsChange.subscribe((value) => this.initTrainingStatus());
  }
 public intentsChange: Subject<object> = new Subject<object>();
   .....

    public updateIntents(appId: number) {
        this.requestsService.getAllIntents(appId)
          .subscribe(intents => {
            this.setIntents(intents);
            this.intentsChange.next(intents);
          });
      }
public-intentchange:Subject=new-Subject();
.....
公共更新内容(appId:number){
this.requestsService.getAllIntents(appId)
.订阅(意向=>{
这个。设置意图(意图);
this.intentchange.next(intents);
});
}
组件测试当前失败,原因是:

  constructor(private updateService: UpdateService) {

    // check if intents are updated in the updateService through upload component, if yes update training status
    this.intentsSubscription = updateService.intentsChange.subscribe((value) => this.initTrainingStatus());
  }
 public intentsChange: Subject<object> = new Subject<object>();
   .....

    public updateIntents(appId: number) {
        this.requestsService.getAllIntents(appId)
          .subscribe(intents => {
            this.setIntents(intents);
            this.intentsChange.next(intents);
          });
      }
TypeError:updateService.IntentChange.subscribe不是函数

description('TrainButtonComponent',()=>{
let组件:TrainButtonComponent;
let夹具:组件夹具;
让mockUpdateService;
让意向改变:主题=新主题();
beforeach(异步(()=>{
意向变更(){
返回({})的主语;
}
};
TestBed.configureTestingModule({
进口:[],
声明:[TrainButtonComponent],
供应商:[
{提供:更新服务,使用值:mockUpdateService}
]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(TrainButtonComponent);
组件=fixture.componentInstance;
mockUpdateService=fixture.debugElement.injector.get(UpdateService);
fixture.detectChanges();
});
它('应该创建',()=>{
expect(component.toBeTruthy();
});
});

是否有人知道如何修复该错误,以便我的测试能够再次运行?

intentchange
您在每次之前的
中声明的内容不做任何操作

您所需要的只是在每次之前正确模拟
updateService

updateService = TestBed.get(UpdateService); // or somehow else, as you wish
spyOn(updateService, 'intentsChange').and.returnValue(Observable.of(<SomeValueHere>)); // <-- here is main point
updateService=TestBed.get(updateService);//或者其他方式,如您所愿
spyOn(updateService,'intentchange')。和.returnValue(Observable.of())//