我有一个使用隔离范围的数据传递给随时间变化的指令的指令.它监视该值的变化,并对每个变化进行一些计算.当我尝试对该指令进行单元测试时,我无法得到手表触发(为简洁起见,但基本概念如下所示):
原文链接:https://www.f2er.com/angularjs/142609.html指示:
@H_404_3@angular.module('directives.file',[]) .directive('file',function() { return { restrict: 'E',scope: { data: '=',filename: '@',},link: function(scope,element,attrs) { console.log('in link'); var convertToCSV = function(newItem) { ... }; scope.$watch('data',function(newItem) { console.log('in watch'); var csv_obj = convertToCSV(newItem); var blob = new Blob([csv_obj],{type:'text/plain'}); var link = window.webkitURL.createObjectURL(blob); element.html('<a href=' + link + ' download=' + attrs.filename +'>Export to CSV</a>'); },true); } }; });测试:
@H_404_3@describe('Unit: File export',function() { var scope; beforeEach(module('directives.file')); beforeEach(inject(function ($rootScope,$compile) { scope = $rootScope.$new(); }; it('should create a CSV',function() { scope.input = someData; var e = $compile('<file data="input" filename="filename.csv"></file>')(scope); //I've also tried below but that does not help scope.$apply(function() { scope.input = {}; }); });我可以做什么来触发手表,所以我的“在手表”调试声明被触发?我的“链接”在编译时被触发.