Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullInjectorError: No provider for Store

I'm receiving the following error when running my unit tests:

Error: StaticInjectorError(DynamicTestModule)[BlogService -> Store]: 
  StaticInjectorError(Platform: core)[BlogService -> Store]: 
    NullInjectorError: No provider for Store!

Here is the code in my test file:

import { TestBed, inject } from '@angular/core/testing';

import { BlogService } from './blog.service';

describe('BlogService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [BlogService]
    });
  });

  it('should be created', inject([BlogService], (service: BlogService) => {
    expect(service).toBeTruthy();
  }));
});

I'm not sure why this error is occuring. I thought the 'inject' call instantiates the service.

like image 851
Kode_12 Avatar asked Jun 27 '18 21:06

Kode_12


1 Answers

As stated here

Add following to the specs.ts file:
// Add the import the module from the package 
import { StoreModule } from '@ngrx/store';

   // Add the imported module to the imports array in beforeEach 
   beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        StoreModule.provideStore({})
      ],
      declarations: [
        // The component that's being tested
      ]
    })
    .compileComponents();
  }));

And if you get error Property 'provideStore' does not exist on type 'typeof StoreModule use forRoot instead of provideStore. Also look here and here is similar question here.

Cheers!

like image 173
Laptop The One Avatar answered Sep 16 '22 11:09

Laptop The One