1
我想为我的Angular应用程序编写一些测试。我写我的茉莉/噶/ PhantomJS测试,我不断收到这让我所有的测试失败,这是Error: No provider for SessionUtil!
错误角度测试使用Karma:未捕获(承诺):错误:没有提供者SessionUtil
SessionUtil是一个实用工具类/服务,它拥有众多的公共方法是错误的。在我main.component.ts文件我把它注射到我的construtor像这样
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!
我显然做错了什么。我试图在TestBed.configureTestingModule
对象providers
数组中注入SessionUtil
,但这给了我很多问题。任何人都可以看到我如何注入这不正确?
任何意见将不胜感激!