简体   繁体   English

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

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

I am trying to write some tests for my Angular application. 我正在尝试为Angular应用程序编写一些测试。 I am writing my tests in Jasmin / Karma / PhantomJS and I keep getting an error which makes all my tests fail, this is Error: No provider for SessionUtil! 我正在用Jasmin / Karma / PhantomJS编写测试,但是不断出现错误,这使我的所有测试均失败,这是Error: No provider for SessionUtil!

SessionUtil is a Utility Class / Service that has numerous public methods. SessionUtil是一种实用程序类/服务,具有许多公共方法。 in my main.component.ts file I inject it into my construtor like so 在我的main.component.ts文件中,我像这样将其注入到我的构造器中

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

and this is my test file (not all of it, just where I declare everything and import my services, etc) 这是我的测试文件(不是全部,只是我声明所有内容并导入服务的地方,等等)

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());
}));

when I run my tests I just get the following error and none of my tests run, Uncaught (in promise): Error: No provider for SessionUtil! 当我运行测试时,我仅收到以下错误,并且Uncaught (in promise): Error: No provider for SessionUtil!运行任何测试, Uncaught (in promise): Error: No provider for SessionUtil! I am obviously doing something wrong. 我显然做错了。 I tried to inject the SessionUtil in the TestBed.configureTestingModule object providers array, but this gave me just as many problems. 我试图将SessionUtil注入TestBed.configureTestingModule对象providers数组中,但这给了我很多问题。 Can anyone see how I am injecting this incorrectly? 谁能看到我是如何错误地注射这种药物的?

Any advice would be appreciated! 任何意见,将不胜感激!

{provide: SessionUtil, useValue: sessionUtil}

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

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

相关问题 Angular 2 No Provider或DI错误(未捕获(承诺):错误:No Provider…) - Angular 2 No Provider or DI error (Uncaught (in promise): Error: No provider …) Angular2“t 没有提供者!” 和未捕获(承诺):错误:DI 错误 - Angular2 "No provider for t!" and Uncaught (in promise): Error: DI Error 错误:未捕获(承诺):错误:没有服务提供程序-Angular 4.1.2 - Error: Uncaught (in promise): Error: No provider for Service - Angular 4.1.2 angular 4错误:未捕获(承诺):错误:没有ConnectionBackend的提供程序! 在注入Jsonp时 - angular 4 Error: Uncaught (in promise): Error: No provider for ConnectionBackend! while injecting Jsonp 错误:没有HttpService的提供者! 在Karma测试中 - Error: No provider for HttpService! In Karma test Angular 4 错误:Karma-Jasmine 测试中没有 ChildrenOutletContexts 的提供者 - Angular 4 Error: No provider for ChildrenOutletContexts in Karma-Jasmine Test 未捕获(承诺):错误:没有 GooglePlus 的提供者 - Uncaught (in promise): Error: No provider for GooglePlus 未捕获(在承诺中):错误:没有String的提供者 - Uncaught (in promise): Error: No provider for String 未发现(承诺中):错误:没有凭据提供者? - Uncaught (in promise): Error: No provider for Credentials? Angular 2错误:在Karma-Jasmine测试中没有Http的提供者 - Angular 2 Error: No provider for Http in Karma-Jasmine Test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM