繁体   English   中英

使用Karma的Angular测试中的错误:未捕获(承诺):错误:没有SessionUtil的提供程序

[英]Error in Angular Test using Karma: Uncaught (in promise): Error: No provider for SessionUtil

我正在尝试为Angular应用程序编写一些测试。 我正在用Jasmin / Karma / PhantomJS编写测试,但是不断出现错误,这使我的所有测试均失败,这是Error: No provider for SessionUtil!

SessionUtil是一种实用程序类/服务,具有许多公共方法。 在我的main.component.ts文件中,我像这样将其注入到我的构造器中

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!运行任何测试, Uncaught (in promise): Error: No provider for SessionUtil! 我显然做错了。 我试图将SessionUtil注入TestBed.configureTestingModule对象providers数组中,但这给了我很多问题。 谁能看到我是如何错误地注射这种药物的?

任何意见,将不胜感激!

{provide: SessionUtil, useValue: sessionUtil}

而且您需要使用存根启动sessionUtil

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM