NullInjectorError: StoreRootModule没有提供程序

5
我运行单元测试时遇到了以下错误。

NullInjectorError: R3InjectorError(DynamicTestModule)[StoreFeatureModule - > StoreRootModule - > StoreRootModule]:
  NullInjectorError: No provider
for StoreRootModule!
  error properties: Object({
    ngTempTokenPath: null,
    ngTokenPath: ['StoreFeatureModule', 'StoreRootModule', 'StoreRootModule']
  })
NullInjectorError: R3InjectorError(DynamicTestModule)[StoreFeatureModule - > StoreRootModule - > StoreRootModule]:
  NullInjectorError: No provider
for StoreRootModule!

以下是代码:

import {
  ComponentFixture,
  TestBed,
  fakeAsync,
  tick,
} from '@angular/core/testing';
import {
  MenuComponent
} from './menu.component';
import {
  RouterTestingModule
} from '@angular/router/testing';
import {
  HttpClientTestingModule
} from '@angular/common/http/testing';
import {
  UnitTestingModule
} from 'src/app/shared/unit-testing/unit-testing.module';
import {
  Location
} from '@angular/common';
import {
  MockStore
} from '@ngrx/store/testing';
import {
  AppState
} from 'src/app/state';
import {
  provideMockStore
} from '@ngrx/store/testing';
import {
  CUSTOM_ELEMENTS_SCHEMA
} from '@angular/core';


describe('MenuComponent', (): void => {
  let component: MenuComponent;
  let fixture: ComponentFixture < MenuComponent > ;
  let location: Location;
  let store: MockStore < AppState > ;

  beforeEach(async(): Promise < void > => {
    await TestBed.configureTestingModule({
        schemas: [CUSTOM_ELEMENTS_SCHEMA],
        imports: [RouterTestingModule, HttpClientTestingModule, UnitTestingModule],
        declarations: [MenuComponent],
        providers: [provideMockStore({})],
      })
      .compileComponents();
    location = TestBed.get(Location);
    store = TestBed.inject(MockStore);
  });

  beforeEach((): void => {
    fixture = TestBed.createComponent(MenuComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    spyOn(store, 'dispatch').and.callFake(() => {});
  });

  it('should create', (): void => {
    expect(component).toBeTruthy();
  });



});

不确定为什么会出现这个错误,尝试了所有可能的方法,并没有找到解决storeRooTmodule的多种解决方案。

添加provideMockStore和mockstore仍然会出错。

由于我是测试用例的新手,很难理解这个特定的错误。


1
你能展示一下 UnitTestingModule 吗?我在想 provideMockStore 需要一个带有 initialState 键的对象,它不能为空。https://ngrx.io/api/store/testing/provideMockStore - AliF50
2个回答

3

尝试添加到您的:

提供商:

providers: [
  MockProvider(Store)
]

导入模块:

imports: [
  StoreModule.forRoot({})
]

0
只需在提供者中添加一个空对象providedMockStore即可。
beforeEach(async(() => {
TestBed.configureTestingModule({
  declarations: [PuntersClubJoinClubPageComponent],
  imports: [HttpClientTestingModule],
  providers: [provideMockStore({})],
}).compileComponents();

}));


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接