在我的代码中有一行:
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框架.