Shai M.
Shai M.

Reputation: 1314

Stub angular service in directive unit test

I have a directive as this:

app.directive('myDirective', ['my', function(myService){
return {
    restrict: 'E',
    scope: {
        resourcePath: '='
    },
    priority: 999,
    transclude: true,
    template: '<ng-transclude ng-if="func()"></ng-transclude>',
    link: function($scope){
        $scope.func = function(){
            return permissionsService.checkSomething(true);
        };
    }
};}]);

and this is the service:

angular.module('services.my', []).service('my', ['$http', function myService($http) {

//public:
this.checkSomething = function (param) {
    return param;
};}]);

How can i stub myService and inject it into the directive dependencies?

Upvotes: 1

Views: 181

Answers (1)

Chandermani
Chandermani

Reputation: 42669

For controller it is easy as you create the controller yourself.

For directive dependency you would have to use $provide to override the default implementation and provide a dummy dependency

beforeEach(module(function ($provide) {
    $provide.service('my', function () { 
        this.checkSomething= function() { };
    });
}));

Upvotes: 1

Related Questions