R3InjectorError(DynamicTestModule)[商店->商店]:NullInjectorError:商店没有提供者

时间:2020-07-12 12:19:05

标签: angular unit-testing redux ngrx angular-test

app.component.ts

public static void CollectGarbage()
{
    while (garbage.TryDequeue(out GLObject ressource))
    {
        ressource.Dispose();
    }
}

app.state.ts

...
constructor(private store: Store<fromAppState.AppState>) {}
  ngOnInit() {
    this.store.dispatch(new fromAppActions.Load());
...

app.component.spec.ts

export interface AppState {
  structure: Structure;
  buttons: number[];
  bars: Bar[];
  limit: number;
  isLoading: boolean;
  error: string;
}
export const initialState: AppState = {
  buttons: [],
  structure: null,
  bars: [],
  limit: 0,
  isLoading: false,
  error: '',
};
const getState = createFeatureSelector<AppState>('myApp');
...

app.module.ts

describe('AppComponent', () => {
  const storeMock = jasmine.createSpyObj('Store', ['select']);
let fixture: ComponentFixture<AppComponent>;
  beforeEach(async(() => {
    fixture = TestBed.createComponent(AppComponent);
    storeMock.select.and.returnValue(
      of({
        structure: structure,
        buttons: [45, 23, -8, -12],
        bars: [15, 34, 7, 87],
        limit: 150,
        isLoading: false,
        error: '',
      })
    );
    component = fixture.componentInstance;

    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule,
        MaterialModule,
        StoreModule.forRoot({}, {}),
      ],
      declarations: [AppComponent, FakeLoaderComponent],
      providers: [{ provide: Store, useValue: storeMock }],
    }).compileComponents();
...
}));
it(`should have as title 'title'`, () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app.title).toEqual('title');
  });
}

运行@NgModule({ declarations: [AppComponent, LoaderComponent], imports: [ BrowserModule, AppRoutingModule, BrowserAnimationsModule, MaterialModule, HttpClientModule, StoreModule.forRoot({}, {}), EffectsModule.forRoot([AppEffects]), StoreModule.forFeature('myApp', AppReducer), StoreDevtoolsModule.instrument({ name: 'My DevTools', maxAge: 50, logOnly: environment.production, }), ], providers: [AppService], bootstrap: [AppComponent], }) export class AppModule {} 时出现此错误: 失败:R3InjectorError(DynamicTestModule)[存储->存储]: NullInjectorError:没有商店的提供者! 错误属性:Object({ngTempTokenPath:null,ngTokenPath:['Store','Store']}) 在茉莉花中

我还需要将reducer导入规格文件吗?

1 个答案:

答案 0 :(得分:0)

在配置测试平台后创建组件。

    storeMock.select.and.returnValue(
      of({
        structure: structure,
        buttons: [45, 23, -8, -12],
        bars: [15, 34, 7, 87],
        limit: 150,
        isLoading: false,
        error: '',
      })
    );


    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule,
        MaterialModule,
        StoreModule.forRoot({}, {}),
      ],
      declarations: [AppComponent, FakeLoaderComponent],
      providers: [{ provide: Store, useValue: storeMock }],
    }).compileComponents();

// AFTER configuration


  fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;