Angular 2测试:在jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时时间内未调用异步回调函数。

7

大家好!请分享你们关于修复以下问题的想法。在编写 Angular2 组件测试时,我遇到了这种错误:

错误:超时 - jasmine.DEFAULT_TIMEOUT_INTERVAL 指定的超时时间内未调用异步回调。

被测试的组件是:(很抱歉,这里有些冗长)

测试代码如下:

import { TestBed, ComponentFixture, async, inject } from '@angular/core/testing';
import { DebugElement } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {Injectable} from '@angular/core';

import {FormGroup, FormBuilder, ReactiveFormsModule} from '@angular/forms';

import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/of';

import SignupComponent from './signup.component';
import {UserService} from '../services/user.service';


@Injectable()
export class MockUserService {
    public signup(user: any) {
        return Observable.of({});
    }
}

let component: SignupComponent;
let fixture: ComponentFixture<SignupComponent>;

describe('SignupComponent', () => {
     beforeEach(async(() => {
         TestBed.configureTestingModule({
            declarations: [ SignupComponent ],
            imports: [
                 BrowserModule,
                 ReactiveFormsModule
            ]
        })
        .overrideComponent(SignupComponent, {
            set: {
                templateUrl: 'app/components/signup.component.html'
            }}
        )
        .overrideComponent(SignupComponent, {
            set: {
                providers: [
                    { provide: UserService, useClass: MockUserService },
                ]
            }
        })
        .compileComponents().then(createComponent);
}));

    it('should create an instance', () => {
        expect(component).toBeDefined();
    });
});

 /***** HELPERS *****/
 function createComponent() {
      fixture = TestBed.createComponent(SignupComponent);
      component = fixture.componentInstance;
      fixture.detectChanges();

     return fixture.whenStable().then(() => {
         fixture.detectChanges();
     });
 }

1个回答

4

当你的异步测试用例在 jasmine 默认规定的 5 秒超时时间之后才完成时,就会出现这种情况。这来自于 Jasmine 文档 -

By default jasmine will wait for 5 seconds for an asynchronous spec to 
finish before causing a timeout failure. If the timeout expires before 
done is called, the current spec will be marked as failed and suite 
execution will continue as if done was called.

If specific specs should fail faster or need more time this can be 
adjusted by setting jasmine.DEFAULT_TIMEOUT_INTERVAL around them.

If the entire suite should have a different timeout, 
jasmine.DEFAULT_TIMEOUT_INTERVAL can be set globally, outside of any 
given describe.

这里是链接。请增加异步调用的DEFAULT_TIMEOUT_INTERVAL,以便Jasmine有足够的时间了解调用是否已处理。这可能是因为您指定的组件过于庞大。

1
感谢您的回复_bad_deadpool_。我尝试了您建议的方法,但不幸的是对我没有起作用。 - KostyaNet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接