人!请善意分享您关于修复以下内容的想法。在为Angular2组件编写测试时遇到了这种错误:
错误:超时 - 在jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时时间内未调用异步回调。
正在测试的组件是:(sorry, it is bulky)
测试:
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();
});
}
答案 0 :(得分:3)
当async规范在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.
以下是link。请为异步调用增加DEFAULT_TIMEOUT_INTERVAL
,以便jasmine有足够的时间知道已处理的呼叫。这可能是因为您的组件太笨重(正如您所指定的那样)。