Javascript 单元测试自定义文件输入指令

Javascript 单元测试自定义文件输入指令,javascript,angularjs,unit-testing,angularjs-directive,jasmine,Javascript,Angularjs,Unit Testing,Angularjs Directive,Jasmine,我在angular 1.5.0和Jasmine 2.4的自定义文件指令中遇到了一个很大的问题,我已经看过了 但是,这似乎只适用于原始输入文件字段,而不适用于自定义指令 首先是指令,非常直接的模型分配。我还触发了作用域上的一个外部函数,我确保它在单元测试中,并且没有从中得到任何错误。我不能强迫文件输入发生更改事件 app.directive('fileModel', fileModel); fileModel.$inject = ['$parse', '$log']; function fil

我在angular 1.5.0和Jasmine 2.4的自定义文件指令中遇到了一个很大的问题,我已经看过了

但是,这似乎只适用于原始输入文件字段,而不适用于自定义指令

首先是指令,非常直接的模型分配。我还触发了作用域上的一个外部函数,我确保它在单元测试中,并且没有从中得到任何错误。我不能强迫文件输入发生更改事件

app.directive('fileModel', fileModel);
fileModel.$inject = ['$parse', '$log'];

function fileModel ($parse, $log) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var model = $parse(attrs.fileModel);
            var modelSetter = model.assign;

            element.bind('change', function(){

                scope.$apply(function(){
                    modelSetter(scope, element[0].files);
                    scope.parseFolder(scope.myFolder);
                });
            });
        }
    };
}
这里是单元测试,现在我正试图通过一个按钮触发和事件,因为我无法手动触发事件,但这也不起作用

describe('fileModel', function () {

    var $compile, $rootScope, directiveElem;

    beforeEach(module("LocalModule"));

    beforeEach(function(){

        inject(function(_$compile_, _$rootScope_){
            $compile = _$compile_;
            $rootScope = _$rootScope_;
        });

        directiveElem = getCompiledElement();
    });

    function getCompiledElement(){
        var element = angular.element('<div ng-controller="UploadCtrl as upload"><input id ="upload" type="file" file-model="myFolder"/><button type="button" id="button" ng-click="clickUpload()">Upload</button></div>');
        var compiledElement = $compile(element)($rootScope);
        $rootScope.clickUpload = function(){
            angular.element('#upload').trigger('click');
        };
        $rootScope.$digest();
        return compiledElement;
    }

    it('should have input element', function () {
        var inputElement = directiveElem.find('input');
        expect(inputElement).toBeDefined();
    });

    it('watched the change function', function () {
        var file = {
            name: "test.png",
            size: 500001,
            type: "image/png"
        };

        var fileList = {
            0: file,
            length: 1,
            item: function (index) { return file; }
        };
        var inputElement = directiveElem.find('input');
        var buttonElement = directiveElem.find('#button');
        inputElement.files = fileList;
        directiveElem.triggerHandler({
            type: 'change',
            target: {
                files: fileList
            }
        });
        $rootScope.$digest();
        buttonElement.triggerHandler('click');
    }); 


});
描述('fileModel',函数(){
var$compile、$rootScope、directiveElem;
在每个(模块(“本地模块”))之前;
beforeach(函数(){
注入(函数($compile,$rootScope){
$compile=\$compile;
$rootScope=\u$rootScope;
});
directiveElem=getCompiledElement();
});
函数getCompiledElement(){
var元素=angular.element('Upload');
var compiledElement=$compile(element)($rootScope);
$rootScope.clickUpload=function(){
元素('#upload')。触发器('click');
};
$rootScope.$digest();
返回编译元素;
}
它('应该有输入元素',函数(){
var inputElement=directiveElem.find('input');
expect(inputElement).toBeDefined();
});
它('观察了变化函数',函数(){
变量文件={
名称:“test.png”,
尺寸:500001,
类型:“图像/png”
};
变量文件列表={
0:文件,
长度:1,
项:函数(索引){返回文件;}
};
var inputElement=directiveElem.find('input');
var buttonElement=directiveElem.find(“#button”);
inputElement.files=文件列表;
定向触发处理器({
键入:“更改”,
目标:{
文件:文件列表
}
});
$rootScope.$digest();
ButtoneElement.triggerHandler('click');
}); 
});

我遇到了类似的问题,并遇到了这个问题。我可以通过调用
$rootScope.$apply()来实现这一点
triggerHandler
方法之后,而不是
$rootScope.$digest()