Angular 如何使用对服务上的事件发射器作出反应的订阅进行异步测试?

Angular 如何使用对服务上的事件发射器作出反应的订阅进行异步测试?,angular,unit-testing,jasmine,karma-runner,angular-test,Angular,Unit Testing,Jasmine,Karma Runner,Angular Test,我在为我的组件进行测试时遇到了很多麻烦。特别是我在这次考试中遇到的问题: it('should set its location string to the location of the first added result', async(() => { fixture.detectChanges(); const result1: Result = new Result(1, 1, 1, 1); result1.addressString = 'Test String';

我在为我的组件进行测试时遇到了很多麻烦。特别是我在这次考试中遇到的问题:

it('should set its location string to the location of the first added result', async(() => {
  fixture.detectChanges();
  const result1: Result = new Result(1, 1, 1, 1);
  result1.addressString = 'Test String';
  resultsService.addResult(result1);
  fixture.detectChanges();
  fixture.whenStable().then( () => {
    expect(component.locationString).toBe('Test String');
  });
}));
我不知道如何让我的测试工作,异步测试上的所有资源都不能帮助我理解我必须做什么。 目前,该测试给出以下两个错误: 一,

我假设原因是因为我的组件中有此订阅:

ngOnInit() {
  this.resultSubscription = this.resultsService.resultListEmitter.subscribe(
    (results: Result[]) => {
      if (results.length < 1) {
        this.errorMode = true;
      }
      this.locationString = results[0].addressString;
      this.secondResultLoaded = results.length >= 2;
    }
  );
  this.resultsService.emitResults();
}
如您所见,每当某些内容发生更改时,它都会使用最多两个结果的列表中的数据来发送事件发射器。组件应该对此作出反应

这方面的问题是,当我进行测试时,我很难弄清楚如何异步应对这些排放。 这会导致我的组件测试没有在服务上执行操作后出现的数据,因为它不会等待我的订阅对数据做出反应

Expected undefined to be 'Test String'.
ngOnInit() {
  this.resultSubscription = this.resultsService.resultListEmitter.subscribe(
    (results: Result[]) => {
      if (results.length < 1) {
        this.errorMode = true;
      }
      this.locationString = results[0].addressString;
      this.secondResultLoaded = results.length >= 2;
    }
  );
  this.resultsService.emitResults();
}
resultList: Result[];
resultListEmitter = new EventEmitter<Result[]>();
emitResults() {
  this.resultListEmitter.emit(this.resultList);
}
addResult(result: Result) {
  if (this.resultList.length > 1) {
    console.log('Second result added.');
    this.resultList[1] = result;
  } else {
    this.resultList.push(result);
    console.log('First result added.');

  }
  this.emitResults();
}

/*Delete last result so that a comparison can be done again*/
wipeLastResult() {
  if (this.resultList.length > 0) {
    this.resultList.splice(this.resultList.length - 1, 1);
  }
  this.emitResults();
}

/*Wipe all results to start over*/
wipeAllResults() {
  this.resultList = [];
  this.emitResults();
}