2017-09-13 10 views
1

カルマ/ジャスミンによるAngular4ユニットテストに問題があります。私はPhantomJSブラウザを使ってローカルでテストを行い、すべてうまくいきます。 ログイン-form.component.spec.tsからカルマジャスミン - jasmine.DEFAULT_TIMEOUT_INTERVALで指定されたタイムアウト時間内に非同期コールバックが呼び出されなかった

Stacktrace

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

EVERYテストが同じエラーに

login-がスローされます。しかし、私はエラーを得た(PhantomJS上)ジェンキンスで同じテストを実行しようとすると、私は、このコンポーネントのすべてのテストのために、このエラーを得た理由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

すべてのサービスを嘲笑する必要があります。単純に定義するだけではなく、ジェンキンがAPI呼び出しを行うことを希望します。原因ジェンキンは動作しません非同期とタイムアウトを与えます。

i18nService, 
AuthenticationService, 

これらのサービスは、モックデータで嘲笑される必要があります。

関連する問題

 関連する問題