测试组件时功能存储的 NullInjectorError

NullInjectorError of feature store when testing component

我尝试修复 LoginComponent 的测试。当功能存储(通知)组件从 SharedModule 移动到延迟加载模块时出现错误。我不知道这里有什么问题。我用 ngrx 测试了测试组件的许多配置。此外,由于进行了强重构,后退 NotificationModule(使用功能存储)并不能修复错误。

describe('LoginComponent', () => {
  let component: LoginComponent;
  let fixture: ComponentFixture<LoginComponent>;
  let store: MockStore<State>;


  beforeAll(() => {
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
  });

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        StoreModule.forRoot(),
        HttpClientTestingModule,
        TranslateModule.forRoot(),
        ReactiveFormsModule,
        SharedModule,
      ],
      declarations: [LoginComponent],
      providers: [
        provideMockStore(),
      ]
    })
      .compileComponents();

    fixture = TestBed.createComponent(LoginComponent);
    component = fixture.componentInstance;
    store = TestBed.get(MockStore);

    spyOn(store, 'dispatch');
    fixture.detectChanges();
  }));

在AppModule中导入的LoginModule。

@NgModule({
  imports: [
    CommonModule,
    HttpClientModule,
    ReactiveFormsModule,
    SharedModule,
    TranslateModule,
  ],
  declarations: [
    LoginComponent,
  ],
})
export class LoginModule {}

错误:

Failed: StaticInjectorError(DynamicTestModule)[NotificationEffects -> Actions]: 
      StaticInjectorError(Platform: core)[NotificationEffects -> Actions]: 
        NullInjectorError: No provider for Actions!
    error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ 'NotificationEffects', Function ] })
        at <Jasmine>
        at NullInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:855:1)
        at resolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17514:1)
        at tryResolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17440:1)
        at StaticInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17266:1)
        at resolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17514:1)
        at tryResolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17440:1)
        at StaticInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17266:1)
        at resolveNgModuleDep (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:30393:1)
        at _createClass (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:30470:1)
        at _createProviderInstance (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:30426:1)
    Error: Expected undefined to be truthy.
        at <Jasmine>
        at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/login/login.component.ngrx.spec.ts:67:23)
        at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:359:1)
        at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:308:1)

错误说 NotificationEffects 需要可注入的 Actions 并且模块不知道如何提供它。查看提供的代码提取,不可能知道什么是 Actions 以及应该如何提供。您可以通过查看 NotificationEffects 源代码开始弄明白。