0

我已经订阅了 ngOnInit 上的 observable,现在我正在尝试为此编写测试,但这似乎不起作用。

这是我正在尝试测试的课程

ngOnInit() {
  this.myService.updates$.pipe(takeUntil(unsusbribe$))
   .subscribe(update => {
     this.feeds = update;
  })
}

这是我的单元测试

it('should set feeds when an update is sent', fakeAsync(() => {
const update = 'fakeUpdate';
component.ngOnInit();
  const serviceSpy = spyOn(myService, 'updates$').and.returnValue(timer(500).pipe(mapTo(update)))
tick(500);
expect(serviceSpy).toHaveBeenCalled(); 
}))

此测试失败并出现错误Expected spy updates$ to have been called.

谁能帮我解决这里的问题。

4

1 回答 1

0

使用间谍服务的正确方法。请试试这个。

  it('should set feeds when an update is sent', fakeAsync(() => {
    const update = 'fakeUpdate';
    const serviceSpy = spyOn(myService, 'updates$');
    serviceSpy.and.returnValue(timer(500).pipe(mapTo(update)))
    tick(500);
    expect(serviceSpy).toHaveBeenCalled(); 
  }))
于 2019-03-07T11:30:01.613 回答