Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 具有默认范围的指令的单元测试_Javascript_Angularjs_Unit Testing_Angularjs Directive - Fatal编程技术网

Javascript 具有默认范围的指令的单元测试

Javascript 具有默认范围的指令的单元测试,javascript,angularjs,unit-testing,angularjs-directive,Javascript,Angularjs,Unit Testing,Angularjs Directive,我试图对一个具有默认作用域(scope:false)的指令进行单元测试,但无法注入其控制器的依赖项 var MyApp = angular.module('MyApp',['MyDirectives','MyServices','MyControllers']); 这是指令 var MyDirectives = angular.module('MyDirectives', []); MyDirectives.directive('myAddress', [ '$timeout', functi

我试图对一个具有默认作用域(scope:false)的指令进行单元测试,但无法注入其控制器的依赖项

var MyApp = angular.module('MyApp',['MyDirectives','MyServices','MyControllers']);
这是指令

var MyDirectives = angular.module('MyDirectives', []);
MyDirectives.directive('myAddress', [ '$timeout', function ( $timeout) {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, elm, attr, ctrl) {
            if (!ctrl) {
                return;
            }

            elm.on('focus', function () {
                scope.AppData.ShowSeparateAddress = false;
            });

        }
    };
}]);
beforeEach(module(MyApp));


var element, compiledElement, directiveElement;
var scope, compile, ele,AppData,controller;

beforeEach(inject(function(_$rootScope_, _$compile_){
scope = _$rootScope_.$new();
compile = _$compile_;
}));
 beforeEach(inject(function( _$controller_, _AppData_){

    AppData = _AppData_;
    controller = _$controller_('Step1',{scope:scope, AppData:AppData});
 }));
 function getCompiledElement(ele) {
     element = angular.element(ele);
     compiledElement = compile(element)(scope);
     scope.$digest();
     return compiledElement;
  }
it('should set show separate addrress as false when focussed',function(){
    ele = '<input type="text" data-my-address />';
    directiveElement = getCompiledElement(ele);
    console.log( directiveElement);
  expect( scope.AppData.ShowSeparateAddress ).toBe(false);
});
这是我的控制器

var  MyControllers = angular.module(' MyControllers', []);
MyControllers.controller('Step1', [
'$rootScope', '$scope', 'AppData', function ($rootScope, $scope, AppData) {
  $scope.AppData = AppData.get();
   }
这是我的应用服务

var  MyServices = angular.module(' MyServices', []);
    MyServices.factory('AppData', [function () {
    var data;
    return {
        get: function () {
            data = data || {};
            return data;
        }
    };
   }]);
下面是地址指令的单元测试

var MyDirectives = angular.module('MyDirectives', []);
MyDirectives.directive('myAddress', [ '$timeout', function ( $timeout) {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, elm, attr, ctrl) {
            if (!ctrl) {
                return;
            }

            elm.on('focus', function () {
                scope.AppData.ShowSeparateAddress = false;
            });

        }
    };
}]);
beforeEach(module(MyApp));


var element, compiledElement, directiveElement;
var scope, compile, ele,AppData,controller;

beforeEach(inject(function(_$rootScope_, _$compile_){
scope = _$rootScope_.$new();
compile = _$compile_;
}));
 beforeEach(inject(function( _$controller_, _AppData_){

    AppData = _AppData_;
    controller = _$controller_('Step1',{scope:scope, AppData:AppData});
 }));
 function getCompiledElement(ele) {
     element = angular.element(ele);
     compiledElement = compile(element)(scope);
     scope.$digest();
     return compiledElement;
  }
it('should set show separate addrress as false when focussed',function(){
    ele = '<input type="text" data-my-address />';
    directiveElement = getCompiledElement(ele);
    console.log( directiveElement);
  expect( scope.AppData.ShowSeparateAddress ).toBe(false);
});
beforeach(模块(MyApp));
var元素、compiledElement、directiveElement;
变量范围、编译、ele、AppData、控制器;
beforeach(注入(函数($rootScope,$compile){
scope=\$rootScope\$new();
compile=\$compile;
}));
每次之前(注入(函数($controller,u AppData){
AppData=\u AppData;
controller=$controller_u('Step1',{scope:scope,AppData:AppData});
}));
函数getCompiledElement(ele){
元素=角度元素(ele);
compiledElement=编译(元素)(范围);
范围。$digest();
返回编译元素;
}
它('聚焦时应将单独的addrress设置为false',函数(){
ele='';
directiveElement=getCompiledElement(ele);
console.log(directiveElement);
expect(scope.AppData.ShowSeparateAddress).toBe(false);
});
})); 我得到以下错误

Error: [$injector:unpr] Unknown provider: AppDataProvider <- AppData

错误:[$injector:unpr]未知提供程序:AppDataProvider控制器签名中有三个参数,只传递两个。您应该添加
$rootScope

controller = _$controller_('Step1',{rootScope:scope, scope:scope, AppData:AppData});

当在浏览器中本地运行时,这真的有效吗?我不确定,但是:您的控制器签名中有三个参数,而您只传递了两个。您应该添加$rootScope@mindparse是的,上述指令运行良好。此指令使用的是默认作用域,而且我在指令中使用的是AppData服务,该指令被注入到名为“Step1”的父控制器中。因此,我无法在指令中模拟此AppData服务。@Zakaria我需要传递控制器构造函数中的每个参数以进行单元测试。如果我的控制器中有大量依赖项,那么传递所有参数是否可行?或者我应该传递需要进行单元测试的参数。