加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular 2测试:在jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时

发布时间:2020-12-17 10:24:23 所属栏目:安全 来源:网络整理
导读:人!请善意分享您关于修复以下内容的想法.在为Angular2组件编写测试时遇到了这种错误: Error: Timeout – Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. 被测组件是:(sorry,it is bulky) 考试: import
人!请善意分享您关于修复以下内容的想法.在为Angular2组件编写测试时遇到了这种错误:

Error: Timeout – Async callback was not invoked within timeout specified by 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();
     });
 }
当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有足够的时间知道已处理的调用.这可能是因为您的组件体积庞大(正如您指定的那样).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读