茉莉花测试错误:超时-在5000毫秒内未调用异步回调(由jasmine.DEFAULT_TIMEOUT_INTERVAL设置)

时间:2019-09-25 17:08:26

标签: angular jasmine

使用茉莉花运行以下角度测试时,我遇到了茉莉花超时异常。有人可以告诉我原因吗?是由于超时配置还是测试不知道何时完成工作。我该如何解决此问题。最初在配置文件中是30000

我试图增加protractor.conf的配置文件中的超时时间

 jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 40000,
    print: function() {}
  },

测试

describe('AppComponent', () => {
    beforeEach(async(() => {
        TestBed.configureTestingModule({
            imports: [
                HttpClientModule,
                FormsModule,
                RouterTestingModule,
                TranslateModule.forRoot({
                    loader: {
                        provide: TranslateLoader,
                        useClass: TranslateLanguageLoader
                    }
                }),
                NgxDatatableModule,
                ToastaModule.forRoot(),
                TooltipModule.forRoot(),
                PopoverModule.forRoot(),
                ModalModule.forRoot()
            ],
            declarations: [
                AppComponent,
                LoginComponent,
                LoadingElementComponent,
                NotificationsViewerComponent
            ],
            providers: [
                InactivityService,
                MonitoringEndpoint,
                MonitoringService,
                AuthService,
                AlertService,
                ConfigurationService,
                AppTitleService,
                AppTranslationService,
                NotificationService,
                NotificationEndpoint,
                AccountService,
                AccountEndpoint,
                LocalStoreManager,
                EndpointFactory,
                TraderActionsService,
                MonitoringService,
                MonitoringEndpoint
            ],
            schemas: [NO_ERRORS_SCHEMA]
        }).compileComponents();
    }));

    it('should create the app', async(() => {
        const fixture = TestBed.createComponent(AppComponent);
        const app = fixture.debugElement.componentInstance;
        expect(app).toBeTruthy()


    }));

    xit(`should have as title 'Argentex'`, async(() => {
        const fixture = TestBed.createComponent(AppComponent);
        const app = <AppComponent>fixture.debugElement.componentInstance;
        expect(app.appTitle).toEqual('Argentex')

    }));

    xit('should render Loaded! in a h1 tag', async(() => {
        const fixture = TestBed.createComponent(AppComponent);
        fixture.detectChanges();
        const compiled = fixture.debugElement.nativeElement;
        expect(compiled.querySelector('h1').textContent).toContain('Loaded!')

    }));
});

1 个答案:

答案 0 :(得分:0)

我注意到您在提供程序中使用了许多服务,并且没有对它们进行模拟,这可能会导致您遇到错误。从官方的Angular文档中:

  

但是,注入真实的服务很少能很好地工作,因为大多数依赖的服务很难创建和控制。

     

相反,您可以模拟依赖项,使用虚拟值或在相关服务方法上创建间谍。

     

首选间谍,因为它们通常是模拟服务的最简单方法。

因此,如果您不需要测试所有这些服务,请对它们进行模拟。

class MockInactivityService {
}

...,然后在提供程序中:

providers: [
    {provide: InactivityService, useClass: MockInactivityService },
    ...
]
相关问题