3

I am trying to mock ElementRef properties while unit testing my angular component and it give me the below error

Error: clientWidth is not declared configurable in http://localhost:9877node_modules/jasmine-core/lib/jasmine-core/jasmine.js (line 4410)

I am getting the error when try to mock the property of native element of an element as below

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 is creating that error.

4

1 回答 1

1

clientWidth并且scrollWidth是 javascript 的只读属性,并且无法使用SpyOn. 因此,您需要使用其他方式通过调用应用程序的其他部分来设置这些只读属性。

于 2017-11-14T20:56:17.723 回答