[英]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.