Angular 为什么不是';我的异步Jasmine单元测试不能工作吗?

Angular 为什么不是';我的异步Jasmine单元测试不能工作吗?,angular,unit-testing,jasmine,Angular,Unit Testing,Jasmine,我正在尝试测试一个异步组件方法,我认为我正确地使用了Angular 4的异步测试功能,但它不起作用。我的问题是,当我运行测试时,它不会等待承诺的解决。函数的异步性质似乎导致超时被触发,测试被提前退出。无论发生什么情况,测试都将通过,因为whenStable()中的所有expect()语句都将被跳过 如果省略async()包装函数,切换到Jasmine语法,传入done回调,并在whenStable()块的末尾调用它,它将正常工作。有人能告诉我为什么Angularasync()wrapper不能使

我正在尝试测试一个异步组件方法,我认为我正确地使用了Angular 4的异步测试功能,但它不起作用。我的问题是,当我运行测试时,它不会等待承诺的解决。函数的异步性质似乎导致超时被触发,测试被提前退出。无论发生什么情况,测试都将通过,因为
whenStable()
中的所有
expect()
语句都将被跳过

如果省略
async()
包装函数,切换到Jasmine语法,传入
done
回调,并在
whenStable()
块的末尾调用它,它将正常工作。有人能告诉我为什么Angular
async()
wrapper不能使用它吗

我的代码如下所示:

// my.component.ts
ngOnInit() {
  this.getResults().then((results) => {
    this.results = results;
  });
}

// My asynchronous function, which takes 1.5s to load results
getResults() {
  let promise = new Promise((resolve) => {
    setTimeout(() => {
      resolve('foo');
    }, 1500);
  })
  return promise;
}


// my.component.spec.ts (Angular async version which doesn't work)
it('should load results', async(() => {
  spyOn(component, 'getResults').and.returnValue(Promise.resolve('bar'));
  component.ngOnInit();

  fixture.whenStable().then(() => {
    // Everything in here gets skipped when I run the test
    fixture.detectChanges();
    // This should cause the test to fail, but it doesn't because it's not run in time
    expect(true).toBe(false)
  });
}));

// my.component.spec.ts (Jasmine version that works)
it('should load results', (done) => {
  spyOn(component, 'getResults').and.returnValue(Promise.resolve('bar'));
  component.ngOnInit();

  fixture.whenStable().then(() => {
    fixture.detectChanges();
    // This should fail and it does because the test works using Jasmine's "done" callback
    expect(true).toBe(false);
    done();
  });
});

感谢@yurzui的Plunker,我确定我的问题是由调用
fixture.detectChanges()
在我的
beforeach()
方法中引起的:

beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [AppComponent],
    });

    fixture = TestBed.createComponent(AppComponent);

    component = fixture.componentInstance;

    // The following line is to blame! Removing it makes the test work correctly.
    fixture.detectChanges();
});

我在处理这个问题时遇到了一些问题,我们通过使用Beforeach中的angular/testing async函数解决了这个问题:

beforeEach(async(() => { // <-- use async
    TestBed.configureTestingModule({
        declarations: [AppComponent],
    });

    fixture = TestBed.createComponent(AppComponent);

    component = fixture.componentInstance;

    // The following line is to blame! Removing it makes the test work correctly.
    fixture.detectChanges();
}));

beforeach(async(()=>{//尝试调用
fixture.detectChanges();
而不是
component.ngOnInit();
,它也将运行
ngOnInit()
,因为这是您第一次调用它。这里怎么了?您
getResult()
方法将不会执行,因为您模拟了it@AmitChigadani谢谢,但这不起作用。
expect(true)。toBe(false)
仍然通过。@yurzui-Aha,你的Plunk帮我解决了这个问题。这就是我正在做的,问题是在我的
beforeach()中调用
fixture.detectChanges()
:你有没有想过为什么
fixture.detectChanges()
不能在
beforeach()
@AdamHughes中,因为我需要阻止
ngOnInit()
运行。
detectChanges()
导致该方法运行。