在测试组件时,出现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();
});
});
我的代码有什么问题或如何解决?有人帮我吗?
答案 0 :(得分:0)
您需要在imports语句中的configureTestingModule中传递存储服务,因为现在您得到的是空服务。函数TestBed.get(store)返回null