大家好!请分享你们关于修复以下问题的想法。在编写 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();
});
}