Angular 角度+;Redux茉莉花测试“;无法读取属性';调度';“未定义”的定义;随机抛出

Angular 角度+;Redux茉莉花测试“;无法读取属性';调度';“未定义”的定义;随机抛出,angular,jasmine,angular-redux,angular-test,ngredux,Angular,Jasmine,Angular Redux,Angular Test,Ngredux,我有一个依赖于2个服务的集成测试,我用存根提供给测试床 当我在这个类别的subscribe块中测试函数updateCategory()时,我有一个函数ngRedux.dispatch({type:something}) 错误:TypeError:无法读取未定义的的属性“dispatch”被随机抛出,即使服务被存根并提供了函数。出于某种奇怪的原因,它认为这是服务的财产 抛出此错误,不对测试进行任何更改,只是刷新karma页面: 每次随机测试 有时,所有的测试都只通过控制台中的错误 有时它在抛出错

我有一个依赖于2个服务的集成测试,我用存根提供给测试床

当我在这个类别的subscribe块中测试函数
updateCategory()
时,我有一个函数ngRedux.dispatch({type:something})

错误:
TypeError:无法读取未定义的
的属性“dispatch”被随机抛出,即使服务被存根并提供了函数。出于某种奇怪的原因,它认为这是服务的财产

抛出此错误,不对测试进行任何更改,只是刷新karma页面:

  • 每次随机测试
  • 有时,所有的测试都只通过控制台中的错误
  • 有时它在抛出错误后停止尝试其他测试
  • 有时,当所有测试都通过时,它抛出错误
这太不可预测了,对我来说毫无意义

试验台配置:

describe('AdminCategoriesComponent', () => {
  let component: AdminCategoriesComponent;
  let fixture: ComponentFixture<AdminCategoriesComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        MatSnackBarModule,
        ReactiveFormsModule,
        FormsModule,
        MatInputModule,
        MatExpansionModule,
        BrowserAnimationsModule,
        NgReduxTestingModule
      ],
      declarations: [AdminCategoriesComponent],
      providers: [
        { provide: AdminService, useClass: AdminStub },
        { provide: NgRedux, useclass: MockNgRedux }
      ],
      schemas: [NO_ERRORS_SCHEMA]
    })
      .compileComponents()
      .then(() => {
        MockNgRedux.reset();
        fixture = TestBed.createComponent(AdminCategoriesComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
      });
  }));
 it('should update category', () => {

    component.categoryID = 1;
    component.categoryTitle = 'Test Category Title';
    component.categoryDescription = 'Test Category Description';
    component.ngOnInit();

    fixture.detectChanges();

    component.categoryForm.value.categoryTitle = 'New Category Title';
    component.categoryForm.value.categoryDescription =
      'New Category Description';

    component.updateCategory();

    fixture.detectChanges();
    expect(component.localLoading).toBeFalsy();
  });

如果没有上面的测试,则不会抛出错误。如果订阅块中没有dispatch函数,也不会抛出错误。

angular redux模块已经过时,通常是垃圾,当您尝试使用它运行任何测试时,这会变得更加明显。在切换到@ngrx/store并正确学习如何使用可观察对象一年后,解决方案是使用@ngrx/store或fork angular redux并自行修复错误。

您能展示一下您的MockNgRedux定义吗?