我有一个使用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
中导入商店。
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();
});