大佬教程收集整理的这篇文章主要介绍了angularjs – 如何在指令中对$emit进行单元测试?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的指令相当简单(为了说明的目的):
angular.module('plunker',[]) .directive('uiList',[function () { return { scope: { lengthModel: '=uiList',},link: function (scope,elm,attrs) { scope.$watch('lengthModel',function (newVal) { scope.$emit('yolo'); }); } } }])
所以每次属性uiList改变时,它都会发出事件.
我的单元测试代码如下:
describe('TesTing $emit in directive',function() { var scope; var element; //you need to inDicate your module in a test beforeEach(function () { module('plunker'); inject(function ($rootScope,$compilE) { scope = $rootScope.$new(); scope.row= 1; spyOn(scope,'$emit'); element = angular.element('<ul id="rows" ui-list="row">'); $compile(element)(scopE); }); }); it('should emit',function() { scope.$digest(); scope.row = 2; scope.$digest(); expect(scope.$emit).toHaveBeenCalledWith("yolo"); }); });
describe('TesTing $emit in directive',function() { var scope; var element; var elementScope; beforeEach(module('plunker')); beforeEach(inject(function($rootScope,$compilE) { scope = $rootScope.$new(); scope.row = 1; element = angular.element('<ul id="rows" ui-list="row">'); $compile(element)(scopE); scope.$digest(); elementScope = element.scope(); })); it('should emit',function() { // arrange spyOn(elementScope,'$emit'); // act scope.$apply(function() { scope.row = 2; }); // assert expect(elementScope.$emit).toHaveBeenCalledWith("yolo"); }); });
以上是大佬教程为你收集整理的angularjs – 如何在指令中对$emit进行单元测试?全部内容,希望文章能够帮你解决angularjs – 如何在指令中对$emit进行单元测试?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。