首页 > 解决方案 > Angular Jest 测试异步代码不是异步的

问题描述

这是一个组件上的方法

public onClick(event: any): void {
    this.isLoading = true;
    this.service.getSomething().subscribe( ("foo") => {
      this.isLoading = false;
    })
}

然后测试

it('test', fakeAsync(()=> {
  component.onClick()
  expect(component.isLoading).toBe(true);
  tick()
  expect(component.isLoading).toBe(false);
}))

它没有说 isLoading 是假的。调试显示代码是按顺序执行的,这意味着在订阅内部的代码执行后返回控件。

标签: angular

解决方案


似乎Observable.of本身不是异步的。你必须让它异步


推荐阅读