我有一个使用ngrx的懒加载Angular 6应用程序。我有一个:

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


核心模块具有页脚和页眉之类的内容。此处没有与store / ngrx相关的内容,但是标头的单元测试(在实现ngrx之前一直有效)一直失败,并显示以下错误:

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

我唯一能想到的是StoreModule没有导入到CoreModule中。但是应该吗?当标头与状态无关时,为什么测试失败?

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

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


shared.module.ts

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


** 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内。

关于javascript - 为什么在使用ngrx的延迟加载的Angular 6应用程序的单元测试中出现“ReducerManager没有提供者!”错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51775859/

10-13 01:05