【发布时间】:2020-05-28 11:26:51
【问题描述】:
在 Angular 中运行 jasmine 单元测试时出现以下错误
错误:
TypeError:无法读取 null 的属性“滚动” 在 在 BoxComponent.ngAfterViewInit (http://localhost:9876/_karma_webpack_/src/app/components/box/box.component.ts:289:14)
// component
ngAfterViewInit() {
document.querySelector('.form-div .form-box').scroll(0, 0);
}
//unit test
it('should call ngAfterViewInit', () => {
component.ngAfterViewInit();
spyOn(document.querySelector('.form-div .form-box'), 'scroll').withArgs(0, 0).and.callThrough();
expect(document.querySelector('.form-div .form-box').scroll).toHaveBeenCalledWith(0, 0);
expect(component.ngAfterViewInit).toBeTruthy();
});
// Error
TypeError: Cannot read property 'scroll' of null
at <Jasmine>
at BoxComponent.ngAfterViewInit (http://localhost:9876/_karma_webpack_/src/app/components/box/box.component.ts:289:14)
【问题讨论】:
标签: angular unit-testing jasmine