我在单元测试我的 Angular 组件时尝试模拟 ElementRef 属性,它给了我以下错误
Error: clientWidth is not declared configurable in http://localhost:9877node_modules/jasmine-core/lib/jasmine-core/jasmine.js (line 4410)
我在尝试模拟元素的 native 元素属性时遇到错误,如下所示
const div = fixture.debugElement.query(By.css('.ellipsis-overflow'));
div.triggerEventHandler('mouseover', null);
fixture.detectChanges();
expect(component.tooltip.isOpen()).toBeFalsy();
spyOnProperty(div.nativeElement, 'clientWidth', 'get').and.returnValue(1400);
spyOnProperty(div.nativeElement, 'scrollWidth', 'get').and.returnValue(2400);
spyOnProperty
正在创建该错误。
最佳答案
clientWidth
和 scrollWidth
是 javascript 的只读属性,无法使用 SpyOn
设置值。因此,您需要通过调用应用程序的其他部分来使用其他方式来设置这些只读属性。
关于angular - 错误 : clientWidth is not declared configurable in http://localhost:9877node_modules/jasmine-core/lib/jasmine-core/jasmine. js(第 4410 行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46819077/