lundi 29 février 2016

unit testing of directive with default scope

I am trying to unit test a directive which has default scope(scope:false) but I am not able to inject dependency of its controller.

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

Here is the directive

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;
            });

        }
    };
}]);

Here is my controller

var  MyControllers = angular.module(' MyControllers', []);
MyControllers.controller('Step1', [
'$rootScope', '$scope', 'AppData', function ($rootScope, $scope, AppData) {
  $scope.AppData = AppData.get();
   }

Here is my App Service

var  MyServices = angular.module(' MyServices', []);
    MyServices.factory('AppData', [function () {
    var data;
    return {
        get: function () {
            data = data || {};
            return data;
        }
    };
   }]);

Here is the unit test for address directive

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);
});

}); I get this following error

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

I have also trieed mocking the service through provide but it did not work Any help or idea ??

Aucun commentaire:

Enregistrer un commentaire