因果报应单元测试'未定义'不是函数错误
Karma unit test 'undefined' is not a function error
我有一个自定义指令,用于检查我想要测试的两个输入字段中的值是否相等(重复密码)。它运行良好,但测试失败,出现以下错误:
TypeError: 'undefined' is not a function (evaluating 'element.add(tween)')
指令非常简单明了:
angular.module('fmAppApp')
.directive('valueMatch', function () {
return {
require: 'ngModel',
restrict: 'A',
link: function postLink(scope, element, attrs, ctrl) {
var tween = '#' + attrs.valueMatch;
element.add(tween).on('keyup', function () {
scope.$apply(function () {
var v = element.val() === $(tween).val();
ctrl.$setValidity('valueMatch', v);
});
});
}
};
});
这是规格:
describe('Directive: valueMatch', function () {
// load the directive's module
beforeEach(module('fmAppApp'));
var element, origElement,
scope;
beforeEach(inject(function ($rootScope) {
scope = $rootScope.$new();
}));
it('should set change validity state of element depending on value of another element',
inject(function ($compile) {
origElement = $compile('<input id="orig">')(scope);
element = $compile('<input value-match="orig" ng-model="repeat">')(scope);
scope.$digest();
expect(element.$valid).toBe(true);
origElement.val('12345');
scope.$digest();
expect(element.$valid).toBe(false);
element.val('12345');
expect(element.$valid).toBe(true);
}));
});
显然add()
是未定义的,对吧?但为什么呢?
我相信在单元测试的情况下,Angular加载的是jqLite,而不是jQuery,它没有add
函数。
要么使用此处描述的jqlite兼容函数之一https://code.angularjs.org/1.2.9/docs/api/angular.element
或者在你的测试中也包括jQuery,在karma配置中。
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- Javascript搜索函数错误
- Chrome Javascript日期构造函数错误
- 浏览器中出现javascript初始大写函数错误
- Createjs冲突问题localToLocal不是函数错误
- Java脚本函数错误
- 更新带有Javascript函数错误的变量
- 如何将对象插入对象列表的开头,当它有 unshift 不是函数错误
- 在将 Pickadate 与 Meteor 一起使用时,未定义不是函数错误
- 运行咕噜咕噜咕噜抛出 _.object 不是函数错误
- 了解函数(错误、数据)回调
- javascript中的typeof()函数错误
- 我一直在获取angularjs服务不是函数错误(LoginService.login不是函数)
- HTML5拖拽&drop正在抛出undefined不是函数错误
- Google Maps API a.lat不是函数错误
- getItemAtIndex不是函数错误
- Javascript停止函数错误
- 对话框函数错误无效函数
- Javascript 自定义函数错误
- 因果报应单元测试'未定义'不是函数错误