Kos*_*Net 6 testing jasmine angular
人!请善意分享您关于修复以下内容的想法.在为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();
});
}
Run Code Online (Sandbox Code Playgroud)
当您的异步规范在 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.
Run Code Online (Sandbox Code Playgroud)
这是相同的链接。请增加DEFAULT_TIMEOUT_INTERVAL异步调用的 ,以便 jasmine 有足够的时间知道调用已得到处理。这可能是因为您的组件体积庞大(正如您所指定的)。