NullInjectorError: No provider for StoreRootModule

Viewed 1629

I have following error while running the unit testcases

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!

below is the code:

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();
  });



});

Not sure why this error is coming tried all possible ways and dint find much solutions ways for storeRooTmodule.

added provideMockStore and mockstore still its giving error

As i am new to test cases getting difficult to understand this particular error.

1 Answers

Try to add to your:

providers:

providers: [
  MockProvider(Store)
]

imports:

imports: [
  StoreModule.forRoot({})
]
Related