angularjs – 如何在Jasmine测试中注入控制器依赖?
发布时间:2020-12-17 07:09:13 所属栏目:安全 来源:网络整理
导读:有以下控制器定义: angular.module('app.controllers',[]).controller('HomeController',[ '$scope','$modal','Point',function($scope,$modal,Point) { //some action } 我想测试这个控制器: describe('HomeController',function() { beforeEach(module('
有以下控制器定义:
angular.module('app.controllers',[]).controller('HomeController',[ '$scope','$modal','Point',function($scope,$modal,Point) { //some action } 我想测试这个控制器: describe('HomeController',function() { beforeEach(module('app.controllers')); var $controller; beforeEach(inject(function(_$controller_){ // The injector unwraps the underscores (_) from around the parameter names when matching $controller = _$controller_; })); describe('$scope.grade',function() { it('sets the strength to "strong" if the password length is >8 chars',function() { var $scope = {}; var controller = $controller('HomeController',{ $scope: $scope }); $scope.label = '12345'; $scope.addNewPoint(); expect($scope.label).toEqual(null); }); }); }); “Point”是我的自定义服务,“$modal”是Angular Bootstrap模块.我怎样才能在测试中注入它?提前致谢! 解决方法
应自动注入服务.如果你想嘲笑他们或监视他们,请像这样注入:
describe('HomeController',function() { beforeEach(module('app')); var $controller,$scope,Point; beforeEach(inject(function(_$controller_,_$rootScope_,_$modal_,_Point_){ $scope = $rootScope.$new(); $modal = _$modal_; Point = _Point_; spyOn($modal,'method'); spyOn(Point,'method'); $controller = _$controller_('HomeController',{ $scope: $scope,$modal: $modal,Point: Point }); })); describe('$scope.grade',function() { $scope.label = '12345'; $scope.addNewPoint(); expect($scope.label).toEqual(null); }); }); }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |