简体   繁体   English

Angular + Redux Jasmine 测试在运行中随机抛出的“无法读取未定义的属性‘调度’”

[英]Angular + Redux Jasmine testing "Cannot read property 'dispatch' of undefined" randomly thrown in runs

I have an integration test that is dependant on 2 services which I am providing to the testbed with stubs.我有一个集成测试,它依赖于我提供给带有存根的测试平台的 2 个服务。

When I am testing a function updateCategory() in the subscribe block of this category I have a function ngRedux.dispatch({type: something})当我在此类别的订阅块中测试函数updateCategory() ,我有一个函数 ngRedux.dispatch({type: something})

The error: TypeError: Cannot read property 'dispatch' of undefined gets randomly thrown out even though the service is stubbed and the function is provided.错误:类型错误: TypeError: Cannot read property 'dispatch' of undefined被随机抛出,即使服务被存根并提供了函数。 For some odd reason it thinks it's a property of the service.出于某种奇怪的原因,它认为这是服务的属性。

This error gets thrown, without making any changes whatsoever to the test, just refreshing the karma page:抛出此错误,无需对测试进行任何更改,只需刷新 karma 页面:

  • At a random test each time.每次随机测试。
  • Sometimes all test pass with the error only in the console.有时所有测试都只在控制台中通过错误。
  • Sometimes it stops trying other tests after throwing the error.有时它会在抛出错误后停止尝试其他测试。
  • Sometimes it throws the error with all tests passing.有时它会在所有测试通过时抛出错误。

It's so unpredictable it just makes no sense to me at all.它是如此不可预测,对我来说根本没有意义。

TestBed Configuration:测试床配置:

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();
      });
  }));

Test configuration:测试配置:

 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();
  });

Without the test above -- the error does not get thrown.如果没有上面的测试——错误不会被抛出。 And without the dispatch function in that subscribe block, the error doesn't get thrown either.如果没有该订阅块中的 dispatch 函数,也不会抛出错误。

angular-redux module is outdated and just generally garbage, this becomes a lot more obvious when you try to run any tests with it. angular-redux 模块已经过时并且通常是垃圾,当您尝试使用它运行任何测试时,这变得更加明显。 A year after switching to @ngrx/store and properly learning how to work with observables, the solution is to use @ngrx/store or fork angular-redux and fix the bugs yourself.在切换到 @ngrx/store 并正确学习如何使用 observable 一年后,解决方案是使用 @ngrx/store 或 fork angular-redux 并自己修复错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular / Redux无法读取未定义的属性“ dispatch” - Angular / Redux Cannot read property 'dispatch' of undefined 茉莉花-“无法读取未定义的抛出属性&#39;subscribe&#39;” - Jasmine - “Cannot read property 'subscribe' of undefined thrown” 茉莉花-“无法读取未定义引发的属性&#39;值&#39;” - Jasmine - “Cannot read property 'values' of undefined thrown” Jasmine/Angular:无法读取未定义的属性“patchValue”(组件测试) - Jasmine/Angular: Cannot read property 'patchValue' of undefined (component testing) 未捕获的TypeError:无法读取未定义引发的属性&#39;coSearchCriteria&#39;-Angular Karma / Jasmine - Uncaught TypeError: Cannot read property 'coSearchCriteria' of undefined thrown - Angular Karma/Jasmine Angular/Jasmine:无法读取未定义的属性“管道” - Angular/Jasmine: Cannot read property 'pipe' of undefined Jasmine - 无法读取undefined的属性&#39;controls&#39; - Angular 6 - Jasmine - Cannot read property 'controls' of undefined - Angular 6 无法读取未定义的 &#39;ngOnInit&#39; &#39;getData&#39;-Angular-Unit testing-jasmine 的属性 - Cannot read property of undefined 'ngOnInit' 'getData'-Angular-Unit testing-jasmine 使用 Karma Jasmine 在 Angular 的单元测试中无法读取未定义的属性“on” - Getting Cannot read property 'on' of undefined in Unit Testing in Angular using Karma Jasmine 类型错误:在读取 ActivatedRoute 的参数时无法读取未定义的属性“0” - Jasmine/Karma 中的 Angular 6 测试 - TypeError: Cannot read property '0' of undefined when reading params of ActivatedRoute - Angular 6 testing in Jasmine/Karma
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM