我在一个有呼叫的控制器中有一个功能
var someVar = angular.element(event.target).scope().field;
我试图通过做
来嘲笑它var ngElementFake = function(el) {
return {
scope: function() {
return {
toggleChildElement: true,
field: scope.field
}
}
}
}
spyOn(angular, 'element').andCallFake(ngElementFake);
但是当我在测试中调用该函数时,我得到了响应:
TypeError: 'undefined' is not a function (evaluating 'injector.get('$rootElement').off()')
at ../angular-mocks/angular-mocks.js:1819
我做错了什么?
编辑:注射
beforeEach(function() {
inject(function($rootScope, $controller) {
scope = $rootScope;
scope.record = recordData;
scope.model = 'Hierarchy';
ctrl = $controller("fngHierarchyChildCtrl", {
$scope: scope
});
});
});
答案 0 :(得分:18)
我能够通过在后回调中手动清除间谍来解决这个问题。
header_redirect
答案 1 :(得分:1)
来自AngularJS FAQ:
由于更改使用on()/ off()而不是bind()/ unbind(),Angular 1.2仅适用于jQuery 1.7.1或更高版本。
因此,尝试升级到jquery 1.7.1或更高版本,或者根本不使用jquery,angular将使用自己的jQLite。
答案 2 :(得分:0)
从角度1.0.8切换到1.2.0时,我在运行测试时遇到以下错误:
TypeError: 'undefined' is not a function (evaluating 'injector.get('$rootElement').off()')
TypeError: 'undefined' is not a function (evaluating '$rootElement.on')
解决方案是编辑业力配置的文件部分并将jQuery移动到angular:
之下 files: [
//was here
'http://code.angularjs.org/1.2.0/angular.js',
'http://code.angularjs.org/1.2.0/angular-mocks.js',
'http://code.angularjs.org/1.2.0/angular-resource.js',
'http://code.angularjs.org/1.2.0/angular-route.js',
'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js', //moved to here
...
]