使用Karma进行角度测试时出错:未捕获(在承诺中):错误:没有SessionUtil的提供者

时间:2017-10-10 14:46:55

标签: angular unit-testing karma-jasmine sinon

我正在尝试为我的Angular应用程序编写一些测试。我正在Jasmin / Karma / PhantomJS中编写我的测试,并且我一直收到错误,导致我的所有测试失败,这是let duration = composition.duration let durationInSeconds = CMTimeGetSeconds(duration) * 10 let composition = AVMutableComposition() let item = AVPlayerItem(asset: composition) let params = AVMutableAudioMixInputParameters(track: composition.tracks.first! as AVAssetTrack) let lastSecond = CMTimeRangeMake(CMTimeMakeWithSeconds(durationInSeconds-10, 10), CMTimeMakeWithSeconds(1, 1)) params.setVolumeRamp(fromStartVolume: 1, toEndVolume: 0, timeRange: lastSecond) let mix = AVMutableAudioMix() mix.inputParameters = [params] item.audioMix = mix // Put the track under the video do { try audioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, current), of: backgroundAudio.tracks(withMediaType: AVMediaTypeAudio)[0], at: kCMTimeZero) } catch _ { print("Failed to load Audio track") } guard let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality) else { return } exporter.audioMix = mix exporter.outputURL = URL(fileURLWithPath: finalVideoPath) exporter.outputFileType = AVFileTypeMPEG4 exporter.shouldOptimizeForNetworkUse = true

SessionUtil是一个具有众多公共方法的实用程序类/服务。在我的main.component.ts文件中,我将其注入我的construtor,如此

Error: No provider for SessionUtil!

这是我的测试文件(不是全部,只是我声明所有内容并导入我的服务等)

constructor(@Inject(SessionUtil) private sessionUtil: SessionUtil) 

当我运行我的测试时,我得到以下错误,并且我的测试都没有运行,describe('MainComponent', () => { let componentFixture: ComponentFixture<MainComponent>; let instance: any; let element: any; let sessionUtil: SessionUtil; let spyLocalizationsService; const firstLanguage = 'en-US'; const secondLanguage = 'de-DE'; const unknownLanguage = 'xx-XX'; beforeEach(async(() => { spyLocalizationsService = sinon.createStubInstance(LocalizationsService); spyLocalizationsService.changeLanguage.withArgs(firstLanguage, sinon.match.any, sinon.match.any).callsArg(1); spyLocalizationsService.changeLanguage.withArgs(secondLanguage, sinon.match.any, sinon.match.any).callsArg(1); spyLocalizationsService.changeLanguage.withArgs(unknownLanguage, sinon.match.any, sinon.match.any).callsArg(2); spyLocalizationsService.getSupportedLanguages.returns([firstLanguage, secondLanguage]); (sinon.stub(spyLocalizationsService, 'currentLanguage') as any).get(() => firstLanguage); // Allows overriding default providers, directives, pipes TestBed.configureTestingModule({ imports: [ RouterTestingModule.withRoutes([ // some Paths ]), TranslateI18NextTestingModule.forRoot(), AuthTestingModule.forRoot(), NoopAnimationsModule, MaterialModule ], declarations: [ MainComponent, DummyComponent ], schemas: [CUSTOM_ELEMENTS_SCHEMA], providers: [{provide: LocalizationsService, useValue: spyLocalizationsService}, {provide: MATERIAL_COMPATIBILITY_MODE, useValue: true}] }).compileComponents().then(() => { componentFixture = TestBed.createComponent(MainComponent); element = componentFixture.nativeElement; instance = componentFixture.componentInstance; // BannerComponent test instance sessionUtil = componentFixture.debugElement.injector.get(SessionUtil); }); })); const isAuthenticated = () => Promise.resolve({ isAuthenticated: true, username: 'Max Musterman' }); describe('on init', () => { beforeEach(async(() => { sinon.stub(sessionUtil, 'isAuthenticated').returns(isAuthenticated()); })); 我显然做错了什么。我试图在Uncaught (in promise): Error: No provider for SessionUtil!对象SessionUtil数组中注入TestBed.configureTestingModule,但这给了我很多问题。任何人都可以看到我错误地注射这个吗?

任何建议都将不胜感激!

1 个答案:

答案 0 :(得分:0)

{provide: SessionUtil, useValue: sessionUtil}

您需要使用存根

启动sessionUtil