`TypeError:无法读取null的属性'dispatchEvent'-用开玩笑进行角度测试

在测试组件时,出现TypeError: Cannot read property 'dispatchEvent' of null错误。这是我的代码:

describe('SubSystemComponent',() => {

    let component: SubSystemComponent;
    let fixture: ComponentFixture<SubSystemComponent>;
    let store: Store<ModelPFServices>;


    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [SubSystemComponent],schemas: [CUSTOM_ELEMENTS_SCHEMA],imports: [
                HttpClientTestingModule,FormsModule,ReactiveFormsModule,StoreModule.forRoot({},{ runtimeChecks: { strictStateImmutability: true,strictactionImmutability: true } }),StoreModule.forFeature('pfservice',reducer),EffectsModule.forRoot([]),EffectsModule.forFeature([EffectsSubSystem])
            ]
        })
        .compileComponents();

        store = TestBed.get(Store);
        spyOn(store,'dispatch').and.callThrough();


    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(SubSystemComponent);
        component = fixture.componentInstance;
        spyOn(component.ssRemoved,'emit');
        fixture.detectChanges();
    });

    it('should create',() => {
        expect(component).toBeTruthy();
    });

    it('should delete a id',() => {
//this part shows error
        const nativeElement = fixture.nativeElement;
        const button = nativeElement.querySelector('.delete-item');
        button.dispatchEvent(new MouseEvent('click'));
        fixture.detectChanges();

    });

});

我的代码有什么问题或如何解决?有人帮我吗?

labkill 回答:`TypeError:无法读取null的属性'dispatchEvent'-用开玩笑进行角度测试

您需要在imports语句中的configureTestingModule中传递存储服务,因为现在您得到的是空服务。函数TestBed.get(store)返回null

本文链接:https://www.f2er.com/3168840.html

大家都在问