使用clientWidth时AngularJS单元测试错误

时间:2022-11-07 13:53:39

In my code there's a line:

在我的代码中有一行:

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

It works fine when running the app but when unit testing I get error:

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

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

How to solve this issue?

如何解决这个问题?

1 个解决方案

#1


1  

Either attach DOM when running test or mock the document.querySelector and angular.element in case you don't need the same.

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

Remember to remove mock/spy on angular element after test is completed as it will interfere with the jasmine framework which uses that internally.

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

#1


1  

Either attach DOM when running test or mock the document.querySelector and angular.element in case you don't need the same.

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

Remember to remove mock/spy on angular element after test is completed as it will interfere with the jasmine framework which uses that internally.

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