0

I have a directive like this:

angular.module('default', []);

angular.module('default').
directive('default', function() 
{

return {
    restrict: 'A',  
    link: function(scope, element, attrs) {      
      element.bind('error', function() {        
        angular.element(this).attr("src", attrs.default);

});

}

}   
});

I want to write unit tests for this directive. I am using karma-jasmine for writing unit tests. How do I go about doing this.

1 Answer 1

1

It's going to be something like this:

describe('default directive', function () {
   it('Should set attribute src to value of attribute default', inject(function ($compile, $rootscope) {
      var scope = $rootscope;
      var elem = angular.element('<div default="test"></div>');
      elem = $compile(elem)(scope);
      expect(elem.children(0)[0].getAttribute('src')).toBe('test');
   }));
});

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi. Thanks @christina. But I didn't follow the expect(elem.children(0)[0].getAttribute('src')).toBe('test'); statement
I have another directive having external html template in it. How to go about testing such a directive?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.