使用clientWidth时AngularJS单元测试错误

前端之家收集整理的这篇文章主要介绍了使用clientWidth时AngularJS单元测试错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的代码中有一行:

var contentWidth = angular.element(document.querySelector('.content'))[0].clientWidth;

它在运行应用程序时工作正常但在单元测试时出现错误

TypeError: 'undefined' is not an object (evaluating 'angular.element(document.querySelector('.content'))[0].clientWidth')

如何解决这个问题?

解决方法

在运行测试时附加DOM,或者在不需要相同的情况下模拟document.querySelector和angular.element.

请记住在测试完成后删除角元素上的mock / spy,因为它会干扰内部使用它的jasmine框架.

猜你在找的Angularjs相关文章