`NullInjectorError:没有存储提供者!

时间:2019-05-30 11:54:56

标签: karma-jasmine angular7 angular-test

如何模拟商店?我正在使用角度规格文件,收到的错误为NullInjectorError: No provider for Store!

如何模拟商店?这是我的测试规格文件:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { HeaderComponent } from './header.component';
import { StateShared } from "./../models/models";
import { StoreModule, Store } from '@ngrx/store';
import { reducerShared } from "./../state/reducers/shared.reducer";
import { Observable, of } from "rxjs";

class MockStore {
  public dispatch(obj) {
    console.log('dispatching from the mock store!')
  }

  public select(obj) {
    console.log('selecting from the mock store!');

    return of({})
  }
}

describe('HeaderComponent', () => {

    let component: HeaderComponent;
    let fixture: ComponentFixture<HeaderComponent>;
    let store:any;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [ HeaderComponent ],
            imports:[StoreModule.forRoot({ reducerShared} )],
            providers:[{provide:Store, useClass: MockStore}]
        })
        .compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(HeaderComponent);
        store = fixture.debugElement.injector.get(Store);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });

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

预先感谢

0 个答案:

没有答案