角度6-我的所有测试失败,并显示“超时-jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时内未调用异步回调”

时间:2018-06-22 16:12:25

标签: node.js angular unit-testing karma-jasmine

我的测试以前可以正常工作,但是升级到Angular 6之后,它们全部失败,并显示相同的错误。 我所有的测试都以相同的结构编写,因此我将其中之一作为示例进行粘贴。 我真的不知道可能是什么问题。我用谷歌搜索并尝试了所有提供的解决方案,但没有一个解决了我的问题。我什至尝试增加量角器的默认超时间隔。在所有测试中我仍然遇到相同的错误。

import {async, ComponentFixture, TestBed, inject} from '@angular/core/testing';
import { HomeComponent } from './home.component';
import {AuthService} from '../login/auth.service';
import {HttpModule} from '@angular/http';
import {VersionValidationDetailsModule} from '../version-validation-details/version-validation-details.module';
import {BrowserModule} from '@angular/platform-browser';
import {VersionValidationService} from '../shared/version-validation.service';
import {FormsModule} from '@angular/forms';
import {RestInfoService} from '../shared/rest.info.service';
import {AuthStoreService} from '../login/auth.store.service';
import {IssueService} from '../shared/issue.service';
import {LocationStrategy, PathLocationStrategy, APP_BASE_HREF} from '@angular/common';
import {RouterTestingModule} from '@angular/router/testing';
import {TestUnitService} from '../shared/test-unit.service';
import {VersionValidationItemComponent} from '../version-validation-item/version-validation-item.component';
import {GqafHeaderComponent} from '../gqaf-header/gqaf-header.component';
import {LoginService} from '../login/login.service';
import {Router} from '@angular/router';
import any = jasmine.any;
import {VersionValidationServiceMock} from '../shared/version-validation.service.mock';
import {AuthGuard} from '../login/auth.guard';



describe('HomeComponent', () => {
  let component: HomeComponent;
  let router: Router;
  let restService: RestInfoService;
  let fixture: ComponentFixture<HomeComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [GqafHeaderComponent, HomeComponent, VersionValidationItemComponent],
      providers: [AuthService, VersionValidationService, AuthStoreService,
        RestInfoService, LoginService, IssueService, TestUnitService, AuthGuard,
        {provide: LocationStrategy, useClass: PathLocationStrategy},
        { provide: APP_BASE_HREF, useValue: '/'},
        {provide: VersionValidationService, useClass: VersionValidationServiceMock}

      ],
      imports: [HttpModule, VersionValidationDetailsModule, BrowserModule, FormsModule,  RouterTestingModule]
    })
      .compileComponents();
    router = TestBed.get(Router);
    restService = TestBed.get(RestInfoService);
    fixture = TestBed.createComponent(HomeComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  }));



  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should add the version to the route when searching', () =>  {
    spyOn(router, 'navigate');
    component.version = 'version1';
    component.searchForVersion();
    fixture.detectChanges();
    expect(router.navigate).toHaveBeenCalledWith([ '/home', 'version1' ] );
  });

  it('should get the version details of the version present in the route',  () =>  {
    spyOn(router, 'navigate');
    spyOn(component.versionValidationService, 'getVersionDetails');
    component.route.snapshot.params['version'] = 'version2';
    component.ngOnInit();
    fixture.detectChanges();
    expect(component.versionValidationService.getVersionDetails).toHaveBeenCalledWith('version2', jasmine.anything(), jasmine.anything());
    expect(router.navigate).toHaveBeenCalledWith([ '/home', 'version2' ] );

  });


  it('should get the version validation Id from the route if available',  () =>  {
    spyOn(router, 'navigate');
    spyOn(component, 'fillVersionValidation');
    component.route.snapshot.params['version'] = 'version3';
    component.route.snapshot.params['versionvalidation'] = 'versionvalidation3';
    component.ngOnInit();
    fixture.detectChanges();
    expect(component.fillVersionValidation).toHaveBeenCalledWith('versionvalidation3');
    expect(router.navigate).toHaveBeenCalledWith([ '/home', 'version3', 'versionvalidation3' ] );
  });

  it('should get the correct list of version validations and display the details of the first',
     () =>  {
    spyOn(router, 'navigate');
    component.version = 'version4';
    component.fillVersionValidation(undefined);
    fixture.detectChanges();
    expect(component.versionValidations.length).toBe(2);
    expect(component.versionValidation.id).toBe('id1');
    expect(component.versionValidation.versionValidationPhases.length).toBe(2);
    });

  it('should get the list of correct list of version validations and display the details of the one passed by the route',
    () =>  {
      spyOn(router, 'navigate');
      component.fillVersionValidation('id2');
      fixture.detectChanges();
      expect(component.versionValidations.length).toBe(2);
      expect(component.versionValidation.id).toBe('id2');
      expect(component.versionValidation.versionValidationPhases.length).toBe(1);
    });

  it('should display an error message when an error occurs while fetching the version details', () =>  {
      spyOn(router, 'navigate');
      component.version = 'testingErrorHandling';
      component.fillVersionValidation('id3');
      fixture.detectChanges();
      expect(component.showErrorMessage).toBeTruthy();
    });

});

这是一个测试示例。 而且我还会添加另一个,以防万一。

import {TestBed, inject, async, ComponentFixture} from '@angular/core/testing';

import {
  MxFieldModule, MxTableModule, MxButtonModule, MxLinkModule, MxMessageModule,
  MxFieldsetModule, MxInputModule} from '@murex/ui';
import {MxFlowLayoutModule} from '@murex/ui/dist/components/flow-layout';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {APP_BASE_HREF} from '@angular/common';
import {LoginComponent} from './login.component';
import {HomeComponent} from '../home/home.component';
import {AuthService} from './auth.service';
import {AppRoutingModule} from '../app-routing.module';
import {HttpModule} from '@angular/http';
import {VersionValidationDetailsModule} from '../version-validation-details/version-validation-details.module';
import {AuthStoreService} from './auth.store.service';
import {RestInfoService} from '../shared/rest.info.service';
import {VersionValidationItemComponent} from '../version-validation-item/version-validation-item.component';
import {GqafHeaderComponent} from '../gqaf-header/gqaf-header.component';
import {LoginService} from './login.service';
import {IssueDetailsComponent} from '../issue-details/issue-details.component';
import {RouterTestingModule} from '@angular/router/testing';
import {Router} from '@angular/router';
import {BreadcrumbsModule} from '../breadcrumbs/breadcrumbs.module';
import {AuthGuard} from './auth.guard';
import {CompareVersionDefectsComponent} from '../compare-version-defects/compare-version-defects.component';
import {WorkflowComponent} from '../workflow/workflow.component';
import {ViewerModule} from 'viewer-mod-lib';


describe('Component: LogIn', () => {
  let component: LoginComponent;
  let fixture: ComponentFixture<LoginComponent>;

  const response = {
    jwt: 'jwt'
  };
      beforeEach(async(() => {

    TestBed.configureTestingModule({
      declarations: [LoginComponent,
        GqafHeaderComponent,
        HomeComponent,
        VersionValidationItemComponent,
        IssueDetailsComponent, CompareVersionDefectsComponent, WorkflowComponent],
      providers: [LoginService,
        RouterTestingModule,
        AuthService,
        AuthStoreService,
        RestInfoService,
        { provide: APP_BASE_HREF, useValue: '/' },
        AuthGuard],
      imports: [AppRoutingModule,
        MxFieldModule,
        MxButtonModule,
        MxLinkModule,
        MxTableModule,
        MxMessageModule,
        MxFlowLayoutModule,
        MxInputModule,
        MxFieldsetModule,
        MxFieldModule,
        FormsModule,
        ReactiveFormsModule,
        HttpModule,
        RouterTestingModule,
        VersionValidationDetailsModule,
        BreadcrumbsModule, ViewerModule]

    });
        fixture = TestBed.createComponent(LoginComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();

  }));

  it('should create log in component', () => {
    // const app = fixture.debugElement.componentInstance;
    expect(component).toBeTruthy();
  });
  it('should create an empty user form', () => {
    fixture.detectChanges();
    expect(component.userForm.value.username).toBe('');
    expect(component.userForm.value.password).toBe('');
  });

  it('should clear the error message when the user types again', () => {
    component.showErrorMessage = true;
    fixture.detectChanges();
    component.userForm.patchValue({username: 'user'});
    fixture.detectChanges();
    expect(component.showErrorMessage).toBeFalsy();
  });
  it('should throw an error if the user was not be authenticated', inject([ RestInfoService], ( restService: RestInfoService) => {
    spyOn(restService, 'authenticateUser').and.callFake(function () {
      arguments[3](response);
    });
    fixture.detectChanges();
    component.userForm.patchValue({username: 'user'});
    component.userForm.patchValue({password: 'password'});
    component.onSignin();
    expect(component.showErrorMessage).toBeTruthy();

  }));

  it('router should navigate to home when the user is authenticated',
    inject([ Router, RestInfoService], ( router: Router, restService: RestInfoService) =>  {
      spyOn(router, 'navigate');
      spyOn(restService, 'authenticateUser').and.callFake(function () {
        arguments[2](response);
      });
      fixture.detectChanges();
      component.userForm.patchValue({username: 'user'});
      component.userForm.patchValue({password: 'password'});
      component.onSignin();
      fixture.detectChanges();
      expect(router.navigate).toHaveBeenCalledWith([ '/home' ] );
    }));

});

请,我真的需要帮助。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我的猜测是,您的请求要执行5秒钟以上,这是查看documentation的默认茉莉花超时。

  

默认情况下,茉莉花5秒等待异步规范   在导致超时失败之前完成操作。如果超时时间在   完成称为完成,当前规范将被标记为失败和套件   执行将继续,就像调用完成一样。

     

如果特定规格失败更快或需要更多时间,可以   通过在它们周围设置jasmine.DEFAULT_TIMEOUT_INTERVAL进行调整。

     

如果整个套件应具有不同的超时时间,   jasmine.DEFAULT_TIMEOUT_INTERVAL可以全局设置,而不是任何   给定描述。

要增加超时,请看这里:

describe("long asynchronous specs", function() {
    var originalTimeout;
    beforeEach(function() {
      originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
      jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
    });

    it("takes a long time", function(done) {
      setTimeout(function() {
        done();
      }, 9000);
    });

    afterEach(function() {
      jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
    });
  });

我认为升级到6号角会降低您的表现。