2017-09-13 117 views
1

我有一个由karma/jasmine进行的Angular4单元测试问题。我在本地运行PhantomJS浏览器的测试,一切都很好。但是当我尝试对詹金斯运行相同的测试(上PhantomJS)我得到错误:Karma Jasmine - 异步回调未在jasmine指定的超时时间内调用.DEFAULT_TIMEOUT_INTERVAL

Stacktrace

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

登录-form.component.spec.ts每个测试抛出了同样的错误

登录 - from.component.spec.ts

describe('LoginFormComponent',() => { 
    let fixture; 
    let submitBtn: DebugElement; 
    let component; 
    let authenticationService: AuthenticationService = null; 
    let backend: MockBackend = null; 
    const requestData = { 
    //mock request data 
    }; 
    beforeEach(async(() => { 
    TestBed.configureTestingModule({ 
     declarations: [ 
     LoginFormComponent, 
     ], 
     imports: [ 
     CommonModule, 
     FormsModule, 
     FormElementsModule, 
     ReactiveFormsModule, 
     RouterTestingModule, 
     TranslateModule, 
     SharedModule, 
     EwniosekSharedModule, 
     Ng2PageScrollModule, 
     ModalModule.forRoot(), 
     VexModalModule, 
     ], 
     providers: [ 
     i18nService, 
     AuthenticationService, 
     BaseRequestOptions, 
     {provide: XHRBackend, useExisting: MockBackend}, 
     { 
      provide: HttpService, 
      useFactory: (backendInstance: XHRBackend, defaultOptions: BaseRequestOptions) => { 
      return new HttpService(backendInstance, defaultOptions); 
      }, 
      deps: [MockBackend, BaseRequestOptions] 
     }, 
     MockBackend 
     ], 
    }).compileComponents(); 
    fixture = TestBed.createComponent(LoginFormComponent); 
    authenticationService = TestBed.get(AuthenticationService); 
    backend = TestBed.get(MockBackend); 
    component = fixture.debugElement.componentInstance; 
    submitBtn = fixture.debugElement.query(By.css('#submitBtn')); 
    })); 

    it('should create this component',() => { 
    expect(component).toBeTruthy(); 
    }); 
    it('should have sumbit button',() => { 
    expect(submitBtn).not.toBeNull(); 
    }); 
    it('should be avaible on /xxx/login url',() => { 
    backend.connections.subscribe((connection: MockConnection) => { 
     const options = new ResponseOptions({ 
     body: JSON.stringify(requestData) 
     }); 
     connection.mockRespond(new Response(options)); 
     expect(connection.request.url).toEqual('/xxx/login'); 
    }); 
    }); 
    it('should click to submit button to login',() => { 
    spyOn<any>(component, 'onSubmit'); 
    expect(fixture.debugElement.query(By.css('#submitBtn'))).toBeDefined(); 
    submitBtn.nativeElement.click(); 
    fixture.detectChanges(); 
    expect(component.onSubmit).toHaveBeenCalled(); 
    }); 
    it('should call login method by URL', (done) => { 
    backend.connections.subscribe((connection: MockConnection) => { 
     const options = new ResponseOptions({ 
     body: JSON.stringify(requestData) 
     }); 
     connection.mockRespond(new Response(options)); 
     expect(connection.request.url).toEqual('/xxx/login'); 
    }); 
    authenticationService.login('TEST', 'xxx').subscribe(
     (res) => { 
     expect(res.username).toContain('TEST'); 
     expect(res.password).toContain('xxx'); 
     expect(res.sex).toContain('male'); 
     done(); 
     } 
    ) 
    }); 
}); 

任何人都可以告诉我为什么我得到这个错误在此组件每一个测试?

回答

3

应删除异步beforeEach(异步(()=> {

2

所有的服务都需要嘲笑,你不能简单地定义它们,希望jenkins可以进行api调用。导致jenkins无法正常工作Async并会发出超时。

i18nService, 
AuthenticationService, 

这些服务需要模拟到mockdata。

相关问题