简体   繁体   English

为什么在使用ngrx的延迟加载的Angular 6应用程序的单元测试中,出现“ ReducerManager没有提供者!”错误?

[英]Why am I getting “No provider for ReducerManager!” error on unit tests for lazy loaded Angular 6 app with ngrx?

I have a lazy loading Angular 6 app that uses ngrx . 我有一个使用ngrx懒加载 Angular 6应用程序。 I have an: 我有一个:

app.module.ts
shared.module.ts
core.module.ts

The core module has things like the footer and header. 核心模块具有页脚和页眉之类的内容。 There are no store/ngrx related stuff here, but the unit tests for the header (that were working before implementing ngrx) keep failing with the following error: 此处没有与store / ngrx相关的内容,但是标头的单元测试(在实现ngrx之前已经起作用)一直失败,并显示以下错误:

StaticInjectorError(Platform: core)[StoreFeatureModule -> ReducerManager]: NullInjectorError: No provider for ReducerManager!

The only thing I can think of is that the StoreModule is not imported into the CoreModule . 我唯一能想到的是StoreModule没有导入到CoreModule But should it? 但是应该吗? Why are the tests failing when the header has nothing to do with state? 当标头与状态无关时,为什么测试失败?

app.module.ts app.module.ts

@NgModule({
declarations: [
    AppComponent,
    LoginComponent,
],
imports: [
    BrowserModule,
    AppRoutingModule,
    CoreModule,
    SharedModule.forRoot(),
    StoreModule.forRoot({}),
    EffectsModule.forRoot([]),
    !environment.production ? StoreDevtoolsModule.instrument({
        maxAge: 5   // retains last 5 states
    }) : []
],
providers: [],
bootstrap: [ AppComponent ]

}) })

core.module.ts core.module.ts

@NgModule({
    imports: [
        CommonModule,
    ],
    exports: [
        CommonModule,
        HeaderComponent,
        FooterComponent
    ],
    declarations: [
        HeaderComponent, 
        FooterComponent, 
    ]
})

shared.module.ts shared.module.ts

@NgModule({
    imports: [
        CommonModule,
        HttpClientModule
    ],
    exports: [
        CommonModule,
        MyComponent
    ],
    declarations: [ MyComponent]
})
export class SharedModule {

** header.component.spec.ts** ** header.component.spec.ts **

describe('HeaderComponent', () => {
    let component: HeaderComponent;
    let fixture: ComponentFixture<HeaderComponent>;
    let debugEl: DebugElement;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            imports: [ 
                ProductsModule,
                CustomersModule
        ],
        declarations: [ HeaderComponent ],
        providers: [
            { provide: myService, useValue: new MyService() },
        ],
    })
    .compileComponents();
}));

beforeEach(() => {
    fixture = TestBed.createComponent(HeaderComponent);
    component = fixture.componentInstance;
    debugEl = fixture.debugElement;
    fixture.detectChanges();
});

因为您的组件使用NgRx存储,所以您还必须将存储导入到TestBed

import { Store, StoreModule } from '@ngrx/store';

describe('test:', () => {
   beforeEach(() => {
   TestBed.configureTestingModule({
   imports: [
    HeldMessagesTabsPageModule,
    StoreModule.forRoot(
      {
        messagesRoot: combineReducers(reducers.reducers)
      },
      {
        initialState: { messagesRoot: { messagesRoot: { template: templateInitialState } } }
      }
    )
  ],

  const store: Store<reducers.State>;
}

beforeEach(() => {
  store = TestBed.get(Store);
  spyOn(store, 'dispatch').and.callThrough();
});

暂无
暂无

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

相关问题 将数组传递给模态时,为什么会出现角度未知的提供者注入器错误? - Why am I getting an angular unknown provider injector error when passing an array to a modal? Angular 4 单元测试,但出现错误 Http 没有提供者 - Angular 4 unit test, but getting error No provider for Http 为什么会出现错误:$ injector:unpr未知提供程序? - Why am i getting Error: $injector:unpr Unknown Provider? 为什么我得到 &#39;:&#39; expected”?我正在尝试定义一些函数来运行一些单元测试以进行基准测试 - Why am I getting ':' expected"? I'm trying to define a few functions to run some unit tests on for a benchmark 延迟加载的功能可以在Angular 6中使用ngrx在功能之间进行通信吗? - Can lazy loaded features communicate state between features with ngrx in Angular 6? 为什么我在webstorm 8中无法从angular获得预包装的错误消息? - Why am I not getting the prepackaged error messages from angular in webstorm 8? 为什么在此单页应用程序中得到$ injector:unpr(未知提供程序)? - Why am I getting $injector:unpr (unknown provider) in this single page app? 为什么我在 angular.min.js 中收到此错误 - Why am i getting this error in the angular.min.js 为什么在angular js中使用resolve时出现错误? - why i am getting error while using resolve in angular js? 为什么我在此React / Typescript应用程序中收到no exported member错误? - Why am I getting the no exported member error in this React/Typescript app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM