Testing ngrx store: No provider for MockStore

Marvin Schön

I am following the Official Documentation for Testing ngrx Stores: https://ngrx.io/guide/store/testing

Even the simplest implementation of injecting a MockStore has the following Error:

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

My code looks like this:

import { TestBed } from '@angular/core/testing';
import { provideMockStore, MockStore } from '@ngrx/store/testing';

describe('Auth Guard', () => {
  // @ts-ignore
  let store: MockStore;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        // any modules needed
      ],
      providers: [
        provideMockStore(),
        // other providers
      ],
    });

    store = TestBed.inject(MockStore);
  });

  it('should create', () => {
    expect(store).toBeTruthy();
  });
});

I am Running @ngrx/[email protected]

satanTime

UPDATED

based on discussion store = TestBed.inject(Store); instead of MockStore is enough for the desired behavior.

ORIGINAL

It is too early,

get it in the test:

  it('should create', inject([Store], (store) => {
    expect(store).toBeTruthy();
  }));

not sure, but you can try to call compileComponents.

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [
        // any modules needed
      ],
      providers: [
        provideMockStore(),
        // other providers
      ],
    }).compileComponents();

    store = TestBed.inject(MockStore);
  });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related