angular - 错误 : clientWidth is not declared configurable in http://localhost:9877node_modules/jasmine-core/lib/jasmine-core/jasmine. js(第 4410 行)

标签 angular karma-jasmine angular-test

我在单元测试我的 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 正在创建该错误。

最佳答案

clientWidthscrollWidth 是 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/

相关文章:

javascript - Angular 单元测试类型错误: Cannot read property 'subscribe' of undefined

javascript - 如何通过 Jasmine spy 将被拒绝/失败的 promise 返回到单元测试中的 Angular Controller

Angular - 测试 Routerguard

angular - 重复的 RxJS 导入警告

angular - 如何在 Angular 中处理来自 @input 的数据?

javascript - 如何在 Angular 中使用 JavaScript 代码?

unit-testing - Karma with Typescript - 无法获得代理来提供我的文件(或者我找不到它)

angular - 如何测试一个简单的 Angular Reactive Form

angular - 单元测试 - onGridReady - Angular 中的 Ag-grid 事件

javascript - 麻烦单元测试 Angular react 性表单字段