大佬教程收集整理的这篇文章主要介绍了初始化 – AngularJS,防止控制器上的init方法在jasmine测试期间启动,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法在单元测试环境中实例化控制器时阻止其调用?
或者也许是一种在webapp上下文中自动调用它而不在控制器代码末尾显式调用init()的方法?
如果您的控制器如下所示:
app.controller('MainCtrl',function($scopE) { $scope.init = function() { //something I really don't want to call during test console.log("I'm execuTing"); }; });
它可以重构为:
app.factory('Initservice',function() { return { init = function() { //something I really don't want to call during test console.log("I'm execuTing"); } }; }); app.controller('MainCtrl',function($scope,InitservicE) { Initservice.init(); });
然后用模拟测试看起来像这样:
describe('TesTing an initializing controller',function() { var $scope,ctrl; //you need to inDicate your module in a test beforeEach(module('plunker')); beforeEach(module(function($providE){ $provide.factory('Initservice',function() { return { init: angular.noop }; }); })); beforeEach(inject(function($rootScope,$controller) { $scope = $rootScope.$new(); ctrl = $controller('MainCtrl',{ $scope: $scope }); })); it('should test sth on a controller',function() { // }); });@H_197_5@
以上是大佬教程为你收集整理的初始化 – AngularJS,防止控制器上的init方法在jasmine测试期间启动全部内容,希望文章能够帮你解决初始化 – AngularJS,防止控制器上的init方法在jasmine测试期间启动所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。