0
votes

I am trying to declare my ngrx/Store into the spec file for testing a container component. So I import the store first and inside the beforeEach do the following:

      import {Store} from '@ngrx/store';

       beforeEach(async(() => { 
        TestBed.configureTestingModule({
              declarations: [NotificationsComponent, NotificationsDevicesComponent, 
               ToArrayPipe, OrderByDate, TimezoneFormatPipe],
              providers: [{ provide: Store, useClass: Store }]
            })
              .compileComponents().then(() => {
                fixture = TestBed.createComponent(NotificationsComponent);
                component = fixture.componentInstance;
                el = fixture.debugElement;

                fixture.detectChanges();
              });
          }));

But I got the error from the heading fails to resolve all parameters for Store: (?, ?, ?).

Any ideas?

Many thanks

1
The cannot resolve parameters for foo: (?, ?) usually means your dependency injection is not working properly (circular dependencies, bad imports, bad exports, etc). The question marks will indicate the dependencies. Sometimes it'll be like (FooService, BarService, ?), which means the third dependency is the one with the problem.Lansana Camara
Try importing the 3 services they you're trying to inject into Store directly from their file, not from a barrel, if that's what you're doing.Lansana Camara

1 Answers

0
votes

I have used ngrx and tested store

Here are the steps I followed -

  1. Imported StoreModule and Store

    import { StoreModule, Store } from '@ngrx/store';
    
  2. Imported my reducers

    import { reducers } from './reducers';
    
  3. Added StoreModule in my TestBed

    TestBed.configureTestingModule({
      imports: [
        StoreModule.forRoot({
          categories: reducers // My reducers
        })
      ],
    
  4. Defined store variable

    let store: Store<CategoryState>;
    
  5. Initialized store

    beforeEach(() => {
      store = TestBed.get(Store);
    
      spyOn(store, 'dispatch').and.callThrough();
    
      fixture = TestBed.createComponent(CategoryListComponent);
      component = fixture.componentInstance;
      fixture.detectChanges();
    });
    
  6. After that you can use store in your test cases.