[英]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 页面:
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.