简体   繁体   English

单元测试角度指令

[英]Unit testing angular directive

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. 我该怎么做。

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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM