NullInjectorError:没有 StoreRootModule 的提供者

问题描述 投票:0回答:2

运行单元测试用例时出现以下错误

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仍然给出错误

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

angular jasmine karma-jasmine
2个回答
3
投票

尝试添加到您的:

提供商

providers: [
  MockProvider(Store)
]

进口

imports: [
  StoreModule.forRoot({})
]

0
投票

只需在providers中添加一个空对象的providedMockStore

beforeEach(async(() => {
TestBed.configureTestingModule({
  declarations: [PuntersClubJoinClubPageComponent],
  imports: [HttpClientTestingModule],
  providers: [provideMockStore({})],
}).compileComponents();

}));

© www.soinside.com 2019 - 2024. All rights reserved.