问题描述
我使用以下ngOnit方法在angular8应用程序中有一个类
ngOnInit(): void {
this.setCustomizedValues();
this.sub = PubSub.subscribe('highlightEntity',(subId,entityIdentifier: string) => {
document.querySelector(entityIdentifier).classList.add('highlight');
setTimeout(() => {
document.querySelector(entityIdentifier).classList.remove('highlight');
},2000);
});
}
我已经初始化了以下测试用例
describe('aComponent',() => {
let httpCallerServiceStub = jasmine.createSpyObj('httpCallerServiceStub',['get']);
let fixture: ComponentFixture<aComponent>;
let componentInstance: aComponent;
localStorage.clear();
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [aComponent],providers: [
{
provide: HttpCallerService,useValue: httpCallerServiceStub
}],imports: [CommonModule,CmcSpinnerModule,NgbModule],}).compileComponents();
fixture = TestBed.createComponent(aComponent);
componentInstance = fixture.componentRef.instance;
fixture.autoDetectChanges();
});
describe('ngOnInit method',() => {
beforeEach(() => {
fixture.componentInstance.customized = {};
});
it('should initialize with minimum input',() => {
// Triggers ngOnInit
fixture.detectChanges();
expect(fixture.componentInstance.customized).toEqual({});
});
});
所以当我运行它时,它会随机抛出以下错误
"message": "An error was thrown in afterAll\nTypeError: Cannot read property 'classList' of null\n at <Jasmine>\n at webpack:///components/aComponent/aComponent.ts:33:53
这是文件的建议
因此,在此之后,无论我提前写了多少测试,问题仍然保持不变,有时可行,有时不可行。
您能告诉我ngonit内部的pubsub.subscriber和超时怎么办吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)