gpt4 book ai didi

Angular + Redux Jasmine 测试 "Cannot read property ' 未定义的调度'在运行中随机抛出

转载 作者:行者123 更新时间:2023-12-02 10:23:26 27 4
gpt4 key购买 nike

我有一个集成测试,该测试依赖于我通过 stub 向测试台提供的 2 个服务。

当我在该类别的订阅 block 中测试函数 updateCategory() 时,我有一个函数 ngRedux.dispatch({type: Something})

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

抛出此错误,无需对测试进行任何更改,只需刷新 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();
});

如果没有上面的测试——就不会抛出错误。如果订阅 block 中没有调度函数,也不会抛出错误。

最佳答案

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

关于Angular + Redux Jasmine 测试 "Cannot read property ' 未定义的调度'在运行中随机抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53395707/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com