0

我在 Angular 中使用 Jasmin、Karma 和 Angular TestBed 进行单元测试。我收到一个错误

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

我正在使用 fakeAsync 和 tick 方法。搜索后,我为我的测试添加了超时参数,甚至还给了 2000 毫秒的时间。

it('Should setup the initial data', fakeAsync(() => {

      fixture.detectChanges();
      tick(2000);

      expect(getUsersSpy).toHaveBeenCalledTimes(1);     

    }), 10000);


 it('Should ensure form is dirty when user changed', fakeAsync(() => {
      fixture.detectChanges();
      tick(2000);

      fixture.whenStable().then(() => {
        userComponentInstance.ngOnInit();        
        userComponentInstance.onUserChanged();
        fixture.detectChanges();        
        expect(userComponentInstance.userForm.form.dirty).toBeTruthy();
      });

 }), 10000);

上面的代码没有运气。根据jasmine:在 jasmine.DEFAULT_TIMEOUT_INTERVAL 指定的超时时间内未调用异步回调我已经尝试过,但它对我不起作用。我没有选择第一个解决方案,因为我正在使用 fakeAsync 并且没有完成

var originalTimeout;

beforeEach(function() {
    originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
});

afterEach(function() {
  jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
});

我在其子组件之一中有以下行。循环创建的子组件

ngAfterViewInit() {
    setTimeout(() => {
        jQuery("#" + this.name).selectpicker();
    },0);        
}
4

0 回答 0