范围函数在 angular 茉莉花测试中给出未定义的错误
scope function is giving not defined error in angular jasmine testing
我在 $scope.getManagerDetails() 中调用函数 child controller.when 我是 运行 我的测试文件对于 child 控制器,我得到 $scope.getManagerDetails is not a function error.Here ManagerController 是 parent 控制器,另一个是 childcontroller.Pleasehelp.Thanks提前
1)管理器控制器
(function(angular){
'use strict'
angular.module('APP').controller('managerCtrl',managerController);
managerController.$inject=['$scope'];
function managerController($scope){
$scope.getManagerDetails = function(){
//some logic
}
}
})(window.angular)
2) 员工控制器
(function(angular){
'use strict'
angular.module('APP').controller('employeeCtrl',employeeController);
employeeController.$inject=['$scope'];
function employeeController($scope){
console.log($scope.getManagerDetails())
}
})(window.angular)
3)员工控制器测试
describe('Employee Controller',function(){
var $scope,
$rootScope,
$injector,
$controller,
employeeController;
beforeEach(module('APP'));
beforeEach(inject(function(_$rootScope_,_$injector_,_$controller_){
$rootScope = _$rootScope_;
$injector = _$injector_;
$controller = _$controller_;
$scope = $rootScope.$new();
employeeController = $controller('employeeCtrl',{
$scope:$scope
});
}));
it('should have controller be defined',function(){
expect(employeeController).toBeDefined();
})
})
getManagerDetails()
未在 Employee Controller 中定义(它在 Manager Controller 中定义)因此您可能希望模拟该函数以及 Employee Controller 具有的任何其他依赖项。例如
$scope = $rootScope.$new();
$scope.getManagerDetails = angular.noop;
也就是说,像这样嵌套控制器并依赖范围继承并不是最佳实践。最好创建一个具有隔离范围的指令,以将所有内容封装在 Employee Controller 和关联的 HTML.
我在 $scope.getManagerDetails() 中调用函数 child controller.when 我是 运行 我的测试文件对于 child 控制器,我得到 $scope.getManagerDetails is not a function error.Here ManagerController 是 parent 控制器,另一个是 childcontroller.Pleasehelp.Thanks提前
1)管理器控制器
(function(angular){
'use strict'
angular.module('APP').controller('managerCtrl',managerController);
managerController.$inject=['$scope'];
function managerController($scope){
$scope.getManagerDetails = function(){
//some logic
}
}
})(window.angular)
2) 员工控制器
(function(angular){
'use strict'
angular.module('APP').controller('employeeCtrl',employeeController);
employeeController.$inject=['$scope'];
function employeeController($scope){
console.log($scope.getManagerDetails())
}
})(window.angular)
3)员工控制器测试
describe('Employee Controller',function(){
var $scope,
$rootScope,
$injector,
$controller,
employeeController;
beforeEach(module('APP'));
beforeEach(inject(function(_$rootScope_,_$injector_,_$controller_){
$rootScope = _$rootScope_;
$injector = _$injector_;
$controller = _$controller_;
$scope = $rootScope.$new();
employeeController = $controller('employeeCtrl',{
$scope:$scope
});
}));
it('should have controller be defined',function(){
expect(employeeController).toBeDefined();
})
})
getManagerDetails()
未在 Employee Controller 中定义(它在 Manager Controller 中定义)因此您可能希望模拟该函数以及 Employee Controller 具有的任何其他依赖项。例如
$scope = $rootScope.$new();
$scope.getManagerDetails = angular.noop;
也就是说,像这样嵌套控制器并依赖范围继承并不是最佳实践。最好创建一个具有隔离范围的指令,以将所有内容封装在 Employee Controller 和关联的 HTML.